Contact Us 1-800-596-4880

flatMap

DataWeave 2.2 is compatible and bundled with Mule 4.2. This version of Mule reached its End of Life on May 2, 2023, when Extended Support ended.

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.

Parameters

Name Description

items

The array to map.

mapper

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

Example

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.

Source

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

Output

[ 3, 5, 0.9, 5.5]

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

Helper function that allows flatMap to work with null values.