4D v16 R6 provides advanced capabilities to manipulate collections, thanks to a series of methods to be used with Object Notation. Over 40 methods are now available, which allow, for example, the ability to add or remove elements in a collection, sort it, search the elements in it and much more! There are so many new possibilities that it is impossible to present them in a single post. This post is the first of a series you don’t want to miss…
How to call Collection methods?
A method associated to a collection is only accessible with Object Notation, thus it’s provided as a preview in 4D v16 R6 . The following example gives you some insight into what the code looks like . For instance, here’s how the push method allows you to add elements to the $col collection.
$col.push(10;100) // Add 10 and 100 to the collection
$s:=$col.sum() // Sum of all the elements of the collection
$r:=$col.pop() // Read and remove the last element
a Vast list of manipulations
- Add elements: push, insert, unshift, resize and fill
- Remove elements: pop, shift, remove and clear
- Sort elements: orderBy, orderByMethod, sort and reverse
- Calculate in collections: countValues, sum, average, count, min, max and reduce
- Compare collections: equal
- Copy and insert in collections: copy, concat, combine, slice and distinct
- Search in collections indexOf, lastIndexOf, find, findIndex, filter, query, indices, every and some
- Extract elements from collections: extract, map
- Manipulate strings: Split string and join
To learn all the details about each method, take a look at the “collections” chapter from the language manual.
CONVERSION TO ARRAYS
For a smooth migration to collections, two new commands, COLLECTION TO ARRAY and ARRAY TO COLLECTION, have been created to help you to write new code alongside your existing code, and easily make them work together!
OK, it was a quite general post, but we had to give you the big picture before going further. The next blog post will go into more detail and demonstrate multiple ways to insert or remove elements from a collection.