# `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:

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

JSON template:

```json
{
    "fields": "${array:forEach(Variables.myArray, '{\"name\": \"${var}\"}')}"
}
```

Returns:

```json
{
    "fields": [{"name": "ProductName"},
               {"name": "Amount"},
               {"name": "CompanyName"},
               {"name": "Revenue"}]
}
```

## `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:

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

Returns:

```json
{
    "fields": [{"index": "1"},
               {"index": "2"},
               {"index": "3"},
               {"index": "4"},
               {"index": "5"}]
}
```

## `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

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

JSON Template:

```json
{
    "concat": "${array:concat(Variables.myArray1,Variables.myArray2)}"
}
```

Returns:

```json
{
    "concat": ["A", "B", "C", "X", "Y", "Z"]
}
```

## `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:

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

JSON template:

```json
{
    "unique": "${array:unique(Variables.myArray}"
}
```

Returns:

```json
{
    "unique": ["ProductName", "Amount", "CompanyName", "Revenue"]
}

```

## `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:

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

JSON template:

```json
{
    "uniqueBy": "${array:uniqueBy('id', Variables.myArray}"
}
```

Returns:

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

```

## `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:

```json
myArray1 = ["ProductName", "Amount", "CompanyName"]
myArray2 = ["Revenue", "Amount", "ProductName"]
```

JSON template:

```json
{
    "union": "${array:union(Variables.myArray1, Variables.myArray2}"
}
```

Returns:

```json
{
    "union": ["ProductName", "Amount", "CompanyName", "Revenue"]
}

```

## `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:

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

JSON template:

```json
{
    "unionBy": "${array:unionBy('id', Variables.myArray1, Variables.myArray2}"
}
```

Returns:

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

## `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:

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

JSON template:

```json
{
    "contains": "${array:contains(Variables.myArray, 'Amount')}"
}
```

Returns:

```json
{
    "contains": true
}

```

## `size`

Parameters: `Collection <Object> array`

Returns: `Integer`

Recursive: Not supported

Description: Return the size of the array.

### Example

Variables:

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

JSON template:

```json
{
    "size": "${array:size(Variables.myArray)}"
}
```

Returns:

```json
{
    "size": 4
}

```

## `last`

Parameters: `Collection <Object> array`

Returns: `Object`

Recursive: Not supported

Description: Return the last item of the array.

### Example

Variables:

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

JSON template:

```json
{
    "last": "${array:last(Variables.myArray)}"
}
```

Returns:

```json
{
    "last": "Revenue"
}
```
