Nav

pluck

pluck({ (K)?: V }, (V, K, Number) → R): Array<R>

Useful for mapping an object into an array, pluck iterates over an input object and returns an array consisting of keys, values, or indices of that object. It is an alternative to mapObject, which is similar but returns an object, instead of an array.

The function can be invoked with any of these parameters: value, key, index. In the next example, 'pluck' iterates over each object within 'prices' and returns arrays of their keys, values, and indices.

If the parameters are not named, the value is defined by default as $, the key as $$, and the index as $$$.

Transform

        
     
1
2
3
4
5
6
7
8
%dw 2.0
output application/json
---
result: {
  keys: payload.prices pluck($$),
  values: payload.prices pluck($),
  indices: payload.prices pluck($$$)
}
Input

        
     
1
2
3
4
5
<prices>
    <basic>9.99</basic>
    <premium>53.00</premium>
    <vip>398.99</vip>
</prices>
Output

        
     
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
{
  'result': {
    'keys': [
      'basic',
      'premium',
      'vip'
    ],
    'values': [
      '9.99',
      '53',
      '398.99'
    ],
    'indices': [
      0,
      1,
      2
    ]
  }
}

You can also use named keys and values as parameters. For example, the next transformation example iterates over the //* prices input above and outputs an array with a single element.

Transform

        
     
1
2
3
4
%dw 2.0
output application/json
---
payload pluck(payload.prices)
Output

        
     
1
2
3
4
5
6
7
[
  {
    'basic': '9.99',
    'premium': '53.00',
    'vip': '398.99'
  }
]

Note that payload pluck(payload.prices) produces the same result as payload pluck(payload[0]).

pluck(Null, (Nothing, Nothing, Nothing) → Any): Null

Helper function that allows pluck to work with null values.