KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: New .at() function for Collection and Entity Selection Class
PRODUCT: 4D | VERSION: 20 | PLATFORM: Mac & Win
Published On: July 11, 2023

As of 4D v20, the .at() function can be called on a Collection or Entity Selection. The .at() function accepts an integer as a parameter and returns the item at the position index. This index that is passed can be a positive or negative integer.

NOTE:
  The negative index must be from (-1 to -N) or else an undefined/Null will be returned.
      • N being the length of the Collection or Entity Selection.

Collection example:

var $collection : Collection
$collection:=New collection(10; 20; 30; 40; 50; 60)

$element0:=$collection.at(0) // 10
$element1:=$collection.at(1) // 20
$element2:=$collection.at(2) // 30
$element3:=$collection.at(-3) // 40
$element4:=$collection.at(-2) // 50
$element5:=$collection.at(-1) // 60

$element6:=$collection.at(6) // undefined
$element7:=$collection.at(-7) // undefined



Similarly, if the index is negative with an Entity Selection, the returned Entity will be based on the reverse order of the Entity Selection.

Note:
  The .at() function will return Null if the index is beyond the entity selection limits.


Entity Selection example:


var $employees : cs.EmployeesSelection
var $employee1; $employee2 : cs.EmployeesEntity

$employees:=ds.Employees.query("firstName = :1";"J@")
$employee1:=$employees.at(3) // 4th entity of the $employees entity selection
$employee2:=$employees.at(-2)
// starting from the end, 2nd entity of the $employees entity selection