%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)
flatten
DataWeave 2.2 is compatible and bundled with Mule 4.2. Standard Support for Mule 4.2 ended on May 2, 2021, and this version of Mule will reach its End of Life on May 2, 2023, when Extended Support ends. Deployments of new applications to CloudHub that use this version of Mule are no longer allowed. Only in-place updates to applications are permitted. MuleSoft recommends that you upgrade to the latest version of Mule 4 that is in Standard Support so that your applications run with the latest fixes and security enhancements. |
flatten(Array<Array<T> | Q>): Array<T | Q>
Turns a nested array (such as [ [1], [2] ]
) into a simple array
(such as [1, 2]
).
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.
Example
This example returns a single array from a nested array of objects.
Source
%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
}
]
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
}
]
]