Tech Tip: Obtain collection of object values from collection of objects using collection.map()
PRODUCT: 4D | VERSION: 19 | PLATFORM: Mac & Win
Published On: November 21, 2022
When working with a collection of objects, sometimes obtaining a collection of values from the objects is desired. This could be done by using a For loop and pushing each object value to a collection, or it can be accomplished more simply by using the collection.map() function. For example, given a collection of objects ($obj_coll): [{“p1:v1”}, {“p2”:“v2”}, {“p3”:“v3”}]
A method named “getValues” can be created like below:
$1.result:=OB Values($1.value)[0] |
Then, the .map() function can be called on the collection with the method as a parameter:
$new_coll:=$obj_coll.map("getValues") // $new_coll returns ["v1","v2","v3"] |
Starting in v19 R6, the Formula command can be used instead of the method:
$new_coll:=$obj_coll.map(Formula($1.result:=OB Values($1.value)[0])) // $new_coll returns ["v1","v2","v3"] |
This is convenient because a new method does not need to be created.