Contact Us 1-800-596-4880

map

DataWeave 2.1 is compatible with Mule 4.1. Standard Support for Mule 4.1 ended on November 2, 2020, and this version of Mule will reach its End of Life on November 2, 2022, when Extended Support ends.

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.

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

Transforms items from the given list (array) into a new list using the specified mapper function.

Parameters

Name Description

items

The array to map.

mapper

Expression or selector used to act on each item and optionally, each index of that item.

Example

This example iterates over an input array (["jose", "pedro", "mateo"]) to produce an array of DataWeave objects. The anonymous function (value, index) → {index: value} maps each item in the input to an object. As {index: value} shows, each index from the input array becomes a key for an output object, and each value of the input array becomes the value of that object.

Source

%dw 2.0
output application/json
---
["jose", "pedro", "mateo"] map (value, index) -> { (index) : value}

Output

[ { "0": "jose" }, { "1": "pedro" }, { "2": "mateo" } ]

Example

This example iterates over the input array (['a', 'b', 'c']) using an anonymous function that acts on the items and indices of the input. For each item in the input array, it concatenates the index + 1 (index plus 1) with an underscore (_), and the corresponding value to return the array, [ "1_a", "2_b", "3_c" ].

Source

%dw 2.0
output application/json
---
['a', 'b', 'c'] map ((value, index) -> (index + 1) ++ '_' ++ value)

Output

[ "1_a", "2_b", "3_c" ]

Example

If the parameters of the mapper function are not named, the index can be referenced with $$, and the value with $. This example iterates over each item in the input array ['joe', 'pete', 'matt'] and returns an array of objects where the index is selected as the key. The value of each item in the array is selected as the value of the returned object. Note that the quotes around $$ are necessary to convert the numbers to strings.

Source

%dw 2.0
output application/json
---
['joe', 'pete', 'matt'] map ( "$$" : $)

Output

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

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

Helper function that allows map to work with null values.