KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Find in array and Count in array Now Work With References to Objects
PRODUCT: 4D | VERSION: 20 R | PLATFORM: Mac & Win
Published On: November 4, 2024

Starting with 4D v20 R6, the functions Find in array and Count in array can now be used on arrays of objects. Now if the developer is storing objects they can find or count the objects using these functions.

These commands specifically are searching for the reference of an object. In the code below it can be noted when searching for the object typed out, it returns -1. This is the case for both Find in array and Count in array.

ARRAY OBJECT($animals; 0)

$ob1:={species: "cat"; type: "mammal"}
$ob2:={species: "dog"; type: "mammal"}
$ob3:={species: "frog"; type: "amphibian"}

APPEND TO ARRAY($animals; $ob1)
APPEND TO ARRAY($animals; $ob2)
APPEND TO ARRAY($animals; $ob3)
APPEND TO ARRAY($animals; $ob1)

$query:=$ob3

//$found ends up being 3 because it's in the 3rd position in the array
$found:=Find in array($animals; $query)

$query:={species: "dog"; type: "mammal"}

//$found2 ends up being -1 because it's not using the reference to an object
$found2:=Find in array($animals; $query)

//$count becomes 2 because there are 2 instances of $ob1 in the array
$count:=Count in array($animals; $ob1)