Contact Free trial Login

pluck

pluck({ (K)?: V }, (value: V, key: K, index: Number) -> R): Array<R>

Useful for mapping an object into an list (array), pluck iterates over an object and returns an array of keys, values, or indices in that object.

It is an alternative to mapObject, which is similar but returns an object, instead of an array.

Parameters

Name Description

object

The object to map.

mapper

The key, value, and/or index (optional) used for mapping the input object.

Example

This example uses pluck to iterate over each element (object) within <prices/> and returns arrays of their keys, values, and indices.

Source

%dw 2.0
output application/json
var readXml = read("<prices>
    <basic>9.99</basic>
    <premium>53.00</premium>
    <vip>398.99</vip>
    </prices>", "application/xml")
---
"result" : {
  "keys" : readXml.prices pluck($$),
  "values" : readXml.prices pluck($),
  "indices" : readXml.prices pluck($$$)
}

Output

{
  "result": {
    "keys": [
      "basic",
      "premium",
      "vip"
    ],
    "values": [
      "9.99",
      "53",
      "398.99"
    ],
    "indices": [
      0,
      1,
      2
    ]
  }
}

Example

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. Note that payload pluck(payload.prices) produces the same result as payload pluck(payload[0]).

Source

%dw 2.0
output application/json
var readXml = read("<prices>
    <basic>9.99</basic>
    <premium>53.00</premium>
    <vip>398.99</vip>
    </prices>", "application/xml")
---
payload pluck(readXml.prices)

Output

[
  {
    "basic": "9.99",
    "premium": "53.00",
    "vip": "398.99"
  }
]

pluck(Null, (value: Nothing, key: Nothing, index: Nothing) -> Any): Null

Helper function that allows pluck to work with null values.

We use cookies to make interactions with our websites and services easy and meaningful, to better understand how they are used and to tailor advertising. You can read more and make your cookie choices here. By continuing to use this site you are giving us your consent to do this.