Contact Us 1-800-596-4880

flatten

flatten<T, Q>(@StreamCapable items: Array<Array<T> | Q>): Array<T | Q>

Turns a set of subarrays (such as [ [1,2,3], [4,5,[6]], [], [null] ]) into a single, flattened array (such as [ 1, 2, 3, 4, 5, [6], null ]).

Note that it flattens only the first level of subarrays and omits empty subarrays.

Parameters

Name Description

items

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

Example

This example defines three arrays of numbers, creates another array containing those three arrays, and then uses the flatten function to convert the array of arrays into a single array with all values.

Source

%dw 2.0
output application/json
var array1 = [1,2,3]
var array2 = [4,5,6]
var array3 = [7,8,9]
var arrayOfArrays = [array1, array2, array3]
---
flatten(arrayOfArrays)

Output

[ 1,2,3,4,5,6,7,8,9 ]

Example

This example returns a single array from nested arrays of objects.

Source

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

Output

[
  {
    "name": "Shoki",
    "id": 5678
  },
  {
    "name": "Mariano",
    "id": 9123
  },
  {
    "name": "Sai",
    "id": 2001
  },
  {
    "name": "Peter",
    "id": 2002
  }
]

Note that if you use myData.user.myarray to select the array of objects in myarray, instead of using flatten(myData.user.myarray), the output is a nested array of objects:

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

flatten(@StreamCapable value: Null): Null

Helper function that enables flatten to work with a null value.