Contact Free trial Login

flatMap

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.

We use cookies to make interactions with our websites and services easy and meaningful, to better understand how they are used and to tailor advertising. You can read more and make your cookie choices here. By continuing to use this site you are giving us your consent to do this.