KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Get a collection of values from an object
PRODUCT: 4D | VERSION: 17 | PLATFORM: Mac & Win
Published On: April 4, 2019
Your current browser may not allow you to download the asset. We recommend Mozilla Firefox or Google Chrome.
Log In

4D has a built-in commend, OB GET PROPERTY NAMES, to retrieve all property names of an object in text array. However, it does not have a similar command to get property values. 4D V17, adds a new iteration structure For each that targets to iterate through collections, entity selections and objects. This provides developer easy access to every property name and value of objects. Below is a method, OB_GetValues, utilizing this new forEach iteration to retrieve a collection of values of all properties of the object:

// Name: OB_GetValues
// Description: Method will return a collection of values from target object
// The order of collection will match the order of array from OB GET PROPERTY NAMES
// Parameter:
// $1 (Object) - target object
//
// Return
// $0(Collection)- a collection of values

C_OBJECT($1)
C_COLLECTION($0)
C_TEXT($property)
$0:=New collection
For each ($property;$1)
  $0.push($1 [$property])
End for each


This method is useful to covert an object to a collection of values. Below is a example:
C_OBJECT($person)
C_COLLECTION($valuesCollection)
$person:=New object()
$person.name:="Susan"
$person.age:=4
$person.registered:=True
$person.family:=New object("Mother";"Kate";"Father";"John";"Brother";"David")
$person.profile:=New object("hobby";New object("Dancing";"Very interested"))
$valuesCollection:=OB_GetValues ($person)
//Result:
//[Susan,4,true,{Mother:Kate,Father:John,Brother:David},{hobby:{Dancing:Very interested}}]