Free MuleSoft CONNECT Keynote & Expo Pass Available!

Register now+
Nav

map

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

Iterates over each item in an array and returns the array of items that results from applying a transformation function to the elements.

The function (a lambda) is invoked with the value and the index parameters. In the following example, custom names are defined for these parameters. Then both are used to construct the returned value. In this case, value is defined as firstName, and its index is defined as position.

Transform

        
     
1
2
3
4
%dw 2.0
output application/json
---
users: ['john', 'peter', 'matt'] map ((firstName, position) -> position ++ ':' ++ upper(firstName))
Output

        
     
1
2
3
4
5
6
7
{
  'users': [
    '0:JOHN',
    '1:PETER',
    '2:MATT'
  ]
}

If the parameters to map are not named, the index is defined by default as $$, and the value is $. The next example produces the same output as the script above. Note that the selector for the key in the next example must be surrounded by parentheses (for example, ($$)).

Transform
%dw 2.0
output application/json
---
users: ['john', 'peter', 'matt'] map (($$) ++ ':' ++ upper($))

This next transformation script produces an array of objects from an input array.

Transform

        
     
1
2
3
4
%dw 2.0
output application/json
---
payload.users map { ($$) : upper($) }
Input
{ 'users' : ['fer', 'steven', 'lorraine', 'george'] }
Output

        
     
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[
  {
    '0': 'FER'
  },
  {
    '1': 'STEVEN'
  },
  {
    '2': 'LORRAINE'
  },
  {
    '3': 'GEORGE'
  }
]
map(Null, (Nothing, Nothing) → Boolean): Null

Helper function that allows map to work with null values.