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.

flatMap(Array<T>, (item: T, index: Number) -> Array<R>): Array<R>

Iterates over each item in an array and flattens the results.

Instead of returning an array of arrays (as map does when you iterate over the values within an input like [ [1,2], [3,4] ]), flatMap returns a flattened array that looks like this: [1, 2, 3, 4]. flatMap is similar to flatten, but flatten only acts on the values of the arrays, while flatMap can act on values and indices of items in the array.


Name Description


The array to map.


Expression or selector for an item and/or index in the array to flatten.


This example returns an array containing each value in order. Though it names the optional index parameter in its anonymous function (value, index) → value, it does not use index as a selector for the output, so it is possible to write the anonymous function using (value) → value. You can also use an anonymous parameter for the value to write the example like this: [ [3,5], [0.9,5.5] ] flatMap $. Note that this example produces the same result as flatten([ [3,5], [0.9,5.5] ]), which uses flatten.


%dw 2.0
output application/json
[ [3,5], [0.9,5.5] ] flatMap (value, index) -> value


[ 3, 5, 0.9, 5.5]

flatMap(Null, (item: Nothing, index: Nothing) -> Any): Null

Helper function that allows flatMap to work with null values.

Was this article helpful?

💙 Thanks for your feedback!

Edit on GitHub
Submit your feedback!
Share your thoughts to help us build the best documentation experience for you!
Take our latest survey!