Contact Free trial Login

flatten

flatten(Array<Array<T> | Q>): Array<T | Q>

Turns a nested array (such as [ [1], [2] ]) into a simple array, such as [1, 2].

Parameters

Name Description

items

The input array of arrays made up of any supported types.

Example

This example flattens an array of arrays.

Source

%dw 2.0
output application/json
---
flatten([ [0.0, 0], [1,1], [2,3], [5,8] ])

Output

[ 0.0, 0 1, 1, 2, 3, 5, 8 ]

Source

This example returns a simple array from a nested array of objects. Note that if you use myData.user.myarray to select the array of objects in myarray, instead of using flatten(myData.user.myarray) to select and then flatten the results, the output will be a nested array of objects.

%dw 2.0
var myData =
{ user : [
   {
     group : "dev",
     myarray : [
       { name : "Shoki", id : 5678 },
       { name : "Mariano", id : 9123 }
     ]
   }
 ]
}
output application/json
---
flatten(myData.user.myarray)

Output

[
  {
    "name": "Shoki",
    "id": 5678
  },
  {
    "name": "Mariano",
    "id": 9123
  }
]

flatten(Null): Null

Helper function that allows flatten to work with null values.