KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Utility Method Utilizing Collection .some() Function
PRODUCT: 4D | VERSION: 19 R 6 | PLATFORM: Mac & Win
Published On: February 13, 2023

The methods below use the .some()function and Formula callback to test collections for a value that meet a specific condition.
.some() function returns true if at least one element in the collection successfully passes the test provided in the formula.

By default .some() will test the whole collection; however, you can pass the index of an element as a parameter, to specify which index you’d like to start from.

The Test example below illustrates how to test the collection, starting from index 1

The Utility Method - someUtility()accepts two parameters: a collection, and a formula object.

#DECLARE($collection : Collection; $formula : Object)->$bool : Boolean

If (Count parameters=2)
   $bool:=$collection.some($formula)
End if


The Utility Method - someUtilityWithIndex() accepts three parameters: the collection, formula object, and integer for the starting index.

#DECLARE($collection : Collection; $formula : Object; $index : Integer)->$bool : Boolean

If (Count parameters=3)
   $bool:=$collection.some($index; $formula)
End if


——Test——
The following code checks if there is a value greater than 0 in a given collection.


var $collection1; $collection2 : Collection
var $test1; $test2; $test3; $test4 : Boolean
var $formula : Object

$formula:=Formula($1.value>0)


$collection1:=New collection(-6; -1; -10; -90; -3; -2)
$test1:=someUtility($collection1; $formula) // false
$collection1.push(1)
$test1:=someUtility($collection1; $formula) //true


$collection2:=New collection(10; -27; -2; 0; -1; -6; -2)
$test2:=someUtility($collection2; $formula) // true
$test2:=someUtilityWithIndex($collection2; $formula; 1) // false
$test3:=$collection2.some(1; $formula) // false


Note: These tests with .some() can be performed directly on the collection with only one line of code, see $test3