---
schema_version: "1"
id: d1975097c3514d337404d0559cb1af5a4a3f19429a913ad750adb5ed4ed7605d
title: flatMap
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/dataweave/latest/dw-core-functions-flatmap.md
canonical_url: https://docs.mulesoft.com/dataweave/latest/dw-core-functions-flatmap
source_site: docs.mulesoft.com
content_type: documentation
content_hash: d86a728f64bf466c6d05a8821202b76b10202f72bc4c9a043b684e665b20d9d0
description: MuleSoft Documentation Site
release: "2.13"
latest: true
---
---
product: DataWeave
version: 2.13
is-latest-version: true
---

# flatMap

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

## 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

<table><colgroup><col> <col></colgroup><thead><tr><th>Name</th><th>Description</th></tr></thead><tbody><tr><td><p><code>items</code></p></td><td><p>The array to map.</p></td></tr><tr><td><p><code>mapper</code></p></td><td><p>Expression or selector for an <code>item</code> and/or <code>index</code> in the array to flatten.</p></td></tr></tbody></table>

### 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

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

#### Output

```JSON
[ 3, 5, 0.9, 5.5]
```

## flatMap<T, R>(@StreamCapable value: Null, mapper: (item: Nothing, index: Nothing) -> Any): Null

Helper function that enables `flatMap` to work with a `null` value.
