Mapping Arrays
2. Under Functions, click array to expand it
3. Click create(item). It appears in the text box above.
4. Click item to replace it with the output element to use to create the array.
5. Click the element in the Upstream Output with which you want to replace item. In the following image, to map strArray, you would need to create an array since there is no array of strings under Upstream Output. So, you map strArray by creating an array. The array.create() function accepts any of the following: a hardcoded string, an element from Upstream Output, an expression, or a function as shown below as long as they all evaluate to the appropriate data type.
Mapping Complex Arrays - Using the array.forEach() Function
Complex arrays are arrays of objects that can optionally contain nested arrays. Complex arrays are mapped using the array.forEach() function. The array.forEach() function can be used with or without arguments.
Refer to https://github.com/TIBCOSoftware/tci-flogo/tree/master/samples/app-dev/
array.forEach.sample for examples.
When you use array.forEach() without any arguments, you define an implicit scope comprising of everything available in the Upstream Output. It is equivalent to creating an implicit array with a single object element comprising of everything in the Upstream Output. Hence, the resulting length of the array is always one element.
To create a confined scope within the Upstream Output, use array.forEach() with arguments. You can do so by entering the mapping manually or by selecting the forEach() function under the array category under Functions. The forEach() function can accept three arguments. When mapping identical arrays, the array.forEach() function gets inserted with the first two arguments by default.
The first argument defines the scope within the Upstream Output. Simply put, the input object or array can only be mapped to elements in the Upstream Output that fall within the boundary indicated by its scope .
The second argument is a scoping variable given to the scope that you have defined in the first
argument. The scoping variable name by default, is the same as the input element name for which you are defining the scope. By doing so, the mapper associates the input object to its scope by the scoping variable. Once there is a scoping variable for the scope, the mapper uses that scoping variable to refer to the scope in future mappings. You can edit the scoping variable to any string that might be more meaningful to you. The scoping variable is particularly useful when mapping the child elements in nested arrays.
The third argument is optional. When iterating through an upstream output array, you can enter a filter to specify a particular condition for mapping as the third argument. When using the filter as the third argument, you must mandatorily enter the scoping variable as the second argument. Only array
elements that match the filter get mapped. For instance, if you are iterating through an array, array1, in the upstream output with a filter that says $loop.name=="Jane" as the third argument, if array1 has ten elements and only four out of them match the condition of the filter, only those four elements will be mapped to the input array and the remaining six will be skipped. This results in the size of the input array to be only four elements, even though array1 has ten elements. See the section, Filtering Array Elements to Map Based on a Condition for more details.
If you have used the array.forEach() in a legacy application, to update your app with the current changes in the array.forEach() function, delete the old mapping and remap the elements. A scoping variable is now included in the mapping. For example, if the old mapping is:
array.forEach($flow.body.Book), after the remap, the mapping should be:
array.forEach($flow.body.Book, "Book") where "Book" is the scoping variable.
Understanding array.ForEach() Function with an Example The example in this section illustrates an array, cakes.
Consider the example below which is available for you to experiment with at https://github.com/
TIBCOSoftware/tci-flogo/tree/master/samples/app-dev/array.forEach.sample.
"batter":[
{
In the above example, the cakes array has two nested arrays called topping and batter. You can use
array.forEach() function to iterate the cakes array or you can also iterate its nested arrays, topping or batter while iterating the cakes array. Basically, you can iterate through nested arrays while iterating through the parent array.
The array.forEach() function can take three arguments:
1. The first argument is the source array to iterate over
2. The second argument is the scope, which typically consists of the array you are iterating over and so has the same name as that array
3. The third optional argument is the condition to use to pull information when looping through the array.
For example, if you want to filter the cakes array based on type "donut", you would use the following expression:
array.forEach($flow.body.cakes,"cakes",$loop.type=="donut")
where
● $ flow.body.cakes is the source array to iterate over.
● "cakes" is the scopeName. This is the scope for your mapping. Each scope has a name to it. By default, the scope name is the same as the name of the source array, in this case "cakes".
● $loop.type=="donut" is the condition to filter the array elements.
The above example displays as follows in the mapper:
To filter the cakes array based on type "donut", you would use the following:
array.forEach($flow.body.cakes,"cakes",$loop.type=="donut")
To filter the batter array inside cakes array based on its type "Regular", use:
array.forEach($loop[cakes].batters.batter,"batter",$loop.type=="Regular")
Here, $loop[cakes] indicates that the cakes parent array is being iterated over and during each iteration of the cakes array, the batter array is also being iterated.
To filter the topping array inside cakes array based on its type "Powdered Sugar", use:
array.forEach($loop[cakes].topping,"topping",$loop.type=="Powdered Sugar")
Here, while iterating the cakes parent array, we are also iterating over the topping array.
Mapping Identical Arrays of Objects
When mapping an array of objects in the input to an identical array of objects (matching property names and data types) in the Upstream Output, keep the following in mind:
● map the array at the root level. The array.forEach() function automatically gets inserted with the array scope and a scoping variable for the scope as its arguments. You need not map the array object properties individually if you want all properties to be mapped and if the object property names are identical. The properties get automatically mapped.
● if you do not want all the properties within the object to be mapped or if the names of object properties do not match, you must map the object properties individually too after mapping the root. If you do not do the child mapping individually, the mismatched properties in the objects remain unmapped if the properties are not marked as required (marked with a red asterisk). If such a property is marked as required, then you see a warning.
● the size of the input array is determined by the size of the array in the Upstream Output to which you are mapping.
To map identical arrays of objects, follow these steps:
Procedure