Contact Free trial Login

flatMap

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

Applies a transformation function to each element in a list (array) and then flattens the result.

Instead of returning an array of arrays (as map does), it returns a flattened array (see the flatten function).

Parameters

Name Description

items

The list (Array type) to map.

mapper

The item and/or index in the list to flatten.

Example

This example returns an array containing each index and item in order. Note that you can also write the example like this: { "users" : ["joe", "pete", "matt"] flatMap ([$$ as String, $]) }

Source

%dw 2.0
output application/json
---
{ "users" : ["joe", "pete", "matt"] flatMap (item, index) -> ([index as String, item]) }

Output

{
   "users": [
     "0",
     "joe",
     "1",
     "pete",
     "2",
     "matt"
   ]
 }

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

Helper function that allows flatMap to work with null values.