KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Utility Method Returns Index based on queryString
PRODUCT: 4D | VERSION: 19 | PLATFORM: Mac & Win
Published On: December 20, 2022

This Utility Method works similar to the .query() function. It returns the indices of the object collection elements that match the queryString conditions and not elements themselves.

The queryString parameter uses the following syntax:
propertyPath comparator value {logicalOperator propertyPath comparator value}

Utility Method getIndices() takes in two parameters
$1 : collection
$2 : queryString
Returns a collection of the indices found

getIndices($1;$2)

#DECLARE($col : Collection; $text : Text)->$result : Collection
If (Count parameters=2)
   $result:=$col.indices($text)
End if


Example
var $col : Collection
$col:=New collection
$col.push(New object("name"; "text1"; "number"; 0))
$col.push(New object("name"; "text2"; "number"; 1))
$col.push(New object("name"; "text3"; "number"; 30))
$col.push(New object("name"; "text4"; "number"; 400))
$col.push(New object("name"; "text5"; "number"; 50000))

$val:=getIndices($col; "number > 2") // [2,3,4]


Note: the indices() function can be used directly on the collection in one line of code. The queryString is passed and the collection is stored in the local variable.
This function does NOT modify the original collection

$val2:=$col.indices("name = :1"; "text4") // [3]