The Array Modules
A common scenario is that data arrives as an array of objects, e.g. from a database query or through an API call. The Crosser module library has many modules to work with arrays, in this article we will go through the main use cases and the modules to use.
When working with arrays there are two main classes of use cases:
- You want to apply some processing to each of the elements in the array
- You want to operate on the whole array
Iterating over array elements
Let’s start with the first class of use cases: Operating on individual array elements. For this use case the ‘Array Split’ module is your best friend. It will break up an array into individual messages, one per element. This will allow you to iterate through the elements and apply whatever processing you like, think a “for loop” in programming languages. When the ‘Array Split’ module receives a message with an array, it will generate a bunch of messages on the output, one for each element. After the ‘Array Split’ module you can then apply any type of processing, by using other modules.
If you want to get the processed data back into an array there is a companion module named ‘Array Join’. This module will collect a number of messages and combine them into a single message with an array. You can configure how to combine messages, e.g. by specifying the number of messages you want to combine, or use a time window to collect messages over. When used in combination with the ‘Array Split’ module there is also an ‘auto’ mode that uses information from the ‘Array Split’ module to recombine the same messages. To make this work you must enable ‘Add array information’ on the ‘Array Split’ module.
Note: If you just want to remove some properties from each element you don’t need to use the above concept, with a Property Mapper in between the ‘Split’ and ‘Join’ modules. There are dedicated modules for that purpose: ‘Array Property Omit/Pick’.
Crosser Example Flow | Array Modules
Operating on arrays
The other class of use cases is when you want to operate on the whole array and either produce new arrays or some value calculated over data in the array elements. Here are some modules that can help you with that:
- Array Filter - Produce a new array with a subset of the elements in the original array, by selecting elements to keep using filter conditions.
- Array GroupBy - Create multiple arrays by grouping array elements with the same value on some property. Similar to SQL Group By.
- Array Statistics - Calculate statistics (min, max, count…) based on the values on some property.
- Array to Object - Combine array elements into an object where ‘key’ and ‘value’ are taken from each element. For example if you want to store an array of data from a PLC module into a database, where tag names are matched against database columns.
- Object to Array - Create an array from an object by converting each property into an array element. Useful for example if you want to apply processing to all properties in an object.