%dw 2.0
output application/json
---
[ [3,5], [0.9,5.5] ] flatMap (value, index) -> value
flatMap
flatMap<T, R>(@StreamCapable items: Array<T>, mapper: (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 |
---|---|
|
The array to map. |
|
Expression or selector for an |
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
.