array Functions

Use array functions in rules to manipulate arrays in asset JSON at runtime.

Array functions can use the answer to a configuration wizard question to set runtime conditions. For example, array functions can check all selected values and set a condition to true or false. They can also take multiple values from an answer and iterate over a JSON array to put those values into the runtime JSON correctly.

Or, say that your wizard contains multiple questions with limited values to choose from, such as Yes/No, or a time frame in days or months. An array function can replace them with one question that asks the user to select multiple features to include in dashboards. In this case, the array function loops over the one question to check for values and set the features that the user picked via a conditional.

forEach 

Parameters: Collection<Object> array, String returnValue

Returns: Collection<Object>

Recursive: Not supported

Description: Evaluates returnValue as an EL expression for each item in the given array and collects each eval result in an array and returns it.

Example 

Variables:

1myArray = ["ProductName","Amount", "CompanyName", "Revenue"]

JSON template:

1{
2    "fields": "${array:forEach(Variables.myArray, '{\"name\": \"${var}\"}')}"
3}

Returns:

1{
2    "fields": [{"name": "ProductName"},
3               {"name": "Amount"},
4               {"name": "CompanyName"},
5               {"name": "Revenue"}]
6}

forEachIndex 

Parameters: Int start, Int end, String returnValue

Returns: Collection<Object>

Recursive: Not supported

Description: Evaluates returnValue as an EL expression for the index number(s) in the array where the string is found.

Example 

JSON template:

1{
2    "fields": "${array:forEachIndex(1, 5, '{\"index\":\"${index}\"}')}"
3}

Returns:

1{
2    "fields": [{"index": "1"},
3               {"index": "2"},
4               {"index": "3"},
5               {"index": "4"},
6               {"index": "5"}]
7}

concat 

Parameters: Collection<Object> array1, Collection<Object> array2

Returns: Collection<Object>

Recursive: Not supported

Description: Returns an array after concatenating the given array1 and array2.

Example 

Variables

1myArray1 = ["A", "B", "C"]
2myArray2 = ["X", "Y", "Z"]

JSON Template:

1{
2    "concat": "${array:concat(Variables.myArray1,Variables.myArray2)}"
3}

Returns:

1{
2    "concat": ["A", "B", "C", "X", "Y", "Z"]
3}

unique 

Parameters: Collection<String> array

Returns: Collection<String>

Recursive: Not supported

Description: Returns an array of unique items. Uniqueness determined by the underlying objects contained in the array. For example, if the collection is an array of strings, all strings with identical values (case-sensitive) are removed.

Example 

Variables:

1myArray = ["ProductName","Amount", "CompanyName", "Revenue", "Amount", "ProductName"]

JSON template:

1{
2    "unique": "${array:unique(Variables.myArray}"
3}

Returns:

1{
2    "unique": ["ProductName", "Amount", "CompanyName", "Revenue"]
3}

uniqueBy 

Parameters: String byFieldName, Collection<Object> array

Returns: Collection<Object>

Recursive: Not supported

Description: Returns an array of unique items. Expects an array of complex objects of similar shape. Uniqueness determined by comparing the value of a field identified by the given byFieldName.

Example 

Variables:

1myArray = [{"name" : "ProductName", "id" : "1"},
2           {"name" : "Amount", "id" : "2"},
3           {"name" : "CompanyName", "id" : "3"},
4           {"name" : "Amount", "id": "2"}]

JSON template:

1{
2    "uniqueBy": "${array:uniqueBy('id', Variables.myArray}"
3}

Returns:

1{
2    "uniqueBy": [{"name" : "ProductName", "id" : "1"},
3                 {"name" : "Amount", "id" : "2"},
4                 {"name" : "CompanyName", "id" : "3"}]
5}

union 

Parameters: Collection<Object> array1, Collection<Object> array2

Returns: Collection<Object>

Recursive: Not supported

Description: Returns a unique union between array1 and array2. Uniqueness determined by the underlying objects contained in the array.

Example 

Variables:

1myArray1 = ["ProductName", "Amount", "CompanyName"]
2myArray2 = ["Revenue", "Amount", "ProductName"]

JSON template:

1{
2    "union": "${array:union(Variables.myArray1, Variables.myArray2}"
3}

Returns:

1{
2    "union": ["ProductName", "Amount", "CompanyName", "Revenue"]
3}

unionBy 

Parameters: String byFieldName, Collection<Object> array1, Collection<Object> array2

Returns: Collection<Object>

Recursive: Not supported

Description: Returns an array which is a union of array1 and array2 items. Expects an array of complex objects of similar shape. Uniqueness determined by comparing the value of a field identified by the given byFieldName.

Example 

Variables:

1myArray1 = [{"name" : "ProductName", "id" : "1"},
2           {"name" : "Amount", "id" : "2"},
3           {"name" : "CompanyName", "id" : "3"}]
4myArray2 = [{"name" : "Revenue", "id" : "4"},
5           {"name" : "Amount", "id" : "2"}]

JSON template:

1{
2    "unionBy": "${array:unionBy('id', Variables.myArray1, Variables.myArray2}"
3}

Returns:

1{
2    "unionBy": [{"name" : "ProductName", "id" : "1"},
3                 {"name" : "Amount", "id" : "2"},
4                 {"name" : "CompanyName", "id" : "3"},
5                 {"name" : "Revenue", "id" : "4"}]
6}

contains 

Parameters: Collection <Object> array, Object item

Returns: Boolean

Recursive: Not supported

Description: Returns true if the given item is in the given array, otherwise false.

Example 

Variables:

1myArray = ["ProductName","Amount", "CompanyName", "Revenue"]

JSON template:

1{
2    "contains": "${array:contains(Variables.myArray, 'Amount')}"
3}

Returns:

1{
2    "contains": true
3}

size 

Parameters: Collection <Object> array

Returns: Integer

Recursive: Not supported

Description: Return the size of the array.

Example 

Variables:

1myArray = ["ProductName","Amount", "CompanyName", "Revenue"]

JSON template:

1{
2    "size": "${array:size(Variables.myArray)}"
3}

Returns:

1{
2    "size": 4
3}

last 

Parameters: Collection <Object> array

Returns: Object

Recursive: Not supported

Description: Return the last item of the array.

Example 

Variables:

1myArray = ["ProductName","Amount", "CompanyName", "Revenue"]

JSON template:

1{
2    "last": "${array:last(Variables.myArray)}"
3}

Returns:

1{
2    "last": "Revenue"
3}