Convert collections

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


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)

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")


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)

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")


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"
//$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.