Tech Tip: Utility Method To Combine Multiple Arrays into a Single Array of Objects
PRODUCT: 4D | VERSION: 15.1 | PLATFORM: Mac & Win
Published On: March 25, 2016
Below is a Utility method that will combine any number of arrays of the same size into an array of objects.
// Util_ArraysToObject // // Desription: // Combines multiple arrays of same size // into an array of objects. // // Parameters: // $1: Pointer to Text Array of Names // ${2}: Pointers to Arrays of Values // ${Count Parameter}: Pointer to Object Array that will contain the result // ---------------------------------------------------- C_POINTER($1;${2}) C_BOOLEAN($match_b) C_LONGINT($count_l;$valueCount_l;$propertyCount_l) C_OBJECT($tempOB) ARRAY OBJECT($resOB;0) If ((Size of array($1->)=(Count parameters-2)) If (Count parameters>2) $match_b:=True For ($count_l;2;Count parameters-2) If (Not(Size of array(${$count_l}->)=Size of array(${$count_l+1}->))) $match_b=False End if End for If ($match_b=True) For($valueCount_l;1;Size of array(${2}->) For ($propertyCount_l;1;Size of array($1->)) OB SET($tempOB;$1->{$propertyCount_l};${$propertyCount_l+1}->{$valueCount_l}) End for APPEND TO ARRAY($resOB;OB Copy($tempOB)) End for COPY ARRAY($resOB;${Count parameters}->) End if End if End if |
Sample of Using the Method:
ARRAY TEXT($label_at;0) ARRAY TEXT($fName;0) ARRAY TEXT($lName;0) ARRAY LONGINT($Age;0) ARRAY OBJECT($ResultOB;0) APPEND TO ARRAY($label_at;"First") APPEND TO ARRAY($label_at;"Last") APPEND TO ARRAY($label_at;"Age") APPEND TO ARRAY($fName;"Joe") APPEND TO ARRAY($fName;"Jane") APPEND TO ARRAY($fName;"Mark") APPEND TO ARRAY($fName;"Sarah") APPEND TO ARRAY($lName;"Andrews") APPEND TO ARRAY($lName;"Chow") APPEND TO ARRAY($lName;"Diaz") APPEND TO ARRAY($lName;"Kim") APPEND TO ARRAY($Age;54) APPEND TO ARRAY($Age;38) APPEND TO ARRAY($Age;61) APPEND TO ARRAY($Age;55) Util_ArraysToObject(->$label_at;->$fName;->$lName;->$Age;->$ResultOB) |
Result: