Convert collections

Automatically translated from English

4D v16 R6 provides advanced capabilities to manipulate collections, thanks to 40+ methods to be used with Object Notation. By using them, you’ll be able to write your code in a more efficient way. New code is great, but we also kept in mind that this new code needs to easily integrate with your existing code. This is why we’ve introduced dedicated commands to convert collections to arrays or strings, and vice-versa.

This article is the second in a series of posts about manipulating collections with the methods added in 4D v16 R6. To explore further, download our database example with 12 different examples demonstrating the myriad possibilities with collection methods!

Database example: Manage collections

Now, let’s focus on converting collections and some code examples.

Arrays and Collections

COLLECTION TO ARRAY

The COLLECTION TO ARRAY command fills an array with elements from a collection:

ARRAY TEXT($artFruits;0)
$fruits:=New collection("Orange";"Banana";"Apple";"Grape")
 
// Convert the $fruits collection to a text array
COLLECTION TO ARRAY($fruits;$artFruits)
//$artFruits{1}="Orange"
//$artFruits{2}="Banana"
//..

The same command can also be used to fill several arrays from a collection of objects:

ARRAY TEXT($city;0)
$col:=New collection(New object("name";"Cleveland";"zc";35049))
$col.push(New object("name";"Blountsville";"zc";35031))
 
// Convert $col collection to 2 arrays
COLLECTION TO ARRAY($col;$city;"name";$zipCode;"zc")
//$city{1}="Cleveland"
//$city{2}="Blountsville"
//...
//$zipCode{1}="35049"
//$zipCode{2}="35031"
//...

ARRAY TO COLLECTION

Correspondingly, you can convert an array to a collection with ARRAY TO COLLECTION:

$colFruits:=New collection
// Convert $artFruits array to a collection
ARRAY TO COLLECTION($colFruits;$artFruits)
//$colFruits=[Orange,Banana,Apple,Grape]

Or fill a collection of objects from several arrays:

$colCity:=New collection
// Convert $city and $zipCode arrays to a collection of objects
ARRAY TO COLLECTION($colCity;$city;"cityName";$zipCode;"Zip")
//$colCity[0]={cityName:Cleveland,Zip:35049}

//$colCity[1]={cityName:Blountsville,Zip:35031}

manipulate strings with collections

The Split string command splits a string into a collection of substrings based on separator characters:

$text:="John;Doe;120 jefferson st.;Riverside;; NJ; 08075"
$col:=Split string($text;";")
//$col:=["John","Doe","120 jefferson st.","Riverside",""," NJ"," 08075"]

You can also modify some collection elements and create a new string using the join method:

$col[2]:="95 S Market St #240"
$col[3]:="San Jose"
$col[5]:="CA"
$col[6]:="95113"
$text:=$col.join(";")
//$text="John;Doe;95 S Market St #240;San Jose;;CA;95113"

There are still more posts to come about manipulating collections, stay tuned!

Fabrice Mainguené

• Product Owner •

Fabrice Mainguené joined 4D Program team in November, 2016. As a Product Owner, he is in charge of writing the user stories then translating it to functional specifications. His role is also to make sure that the feature implementation delivered is meeting the customer need.

After obtaining a Bachelor degree in Computer Science at CNAM, Fabrice joined a small software publishing company as a Windev developer. Then he worked for different companies in industry and trade areas as a Windev and web developer as well as technical advisor on new features.