---
schema_version: "1"
id: 8b19f2f0f1a453b88feb428a593ec8c1cac9366132d9b2531572c974922ed346
title: pluck
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/dataweave/2.4/dw-core-functions-pluck.md
canonical_url: https://docs.mulesoft.com/dataweave/2.4/dw-core-functions-pluck
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 54ad3c394a3c73eed04fa32edb7d2739b410360b595e957ff76708963d91e779
description: MuleSoft Documentation Site
release: "2.4"
latest: false
---
---
product: DataWeave
version: 2.4
---

# pluck

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

## pluck<K, V, R>(@StreamCapable object: { (K)?: V }, mapper: (value: V, key: K, index: Number) -> R): Array<R>

Useful for mapping an object into an array, `pluck` iterates over an object and returns an array of
keys, values, or indices from the object.

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

### Parameters

<table><colgroup><col> <col></colgroup><thead><tr><th>Name</th><th>Description</th></tr></thead><tbody><tr><td><p><code>object</code></p></td><td><p>The object to map.</p></td></tr><tr><td><p><code>mapper</code></p></td><td><p>Expression or selector that provides the <code>key</code>, <code>value</code>, and/or <code>index</code> (optional) used for mapping the specified object into an array.</p></td></tr></tbody></table>

### Example

This example iterates over `{ "a":"b","c":"d"}` using the anonymous mapper function
(`(value,key,index) → { (index) : { (value):key} }`) to invert each key-value pair in the specified
object and to return their indices as keys. The mapper uses named parameters to identify the keys,
values, and indices of the object. Note that you can write the same expression using anonymous
parameters, like this: `{"a":"b","c":"d"} pluck { ($$$) : { ($):$$} }` Unlike the almost identical
example that uses `mapObject`, `pluck` returns the output as an array.

#### Source

```DataWeave
%dw 2.0
output application/json
---
{"a":"b","c":"d"} pluck (value,key,index) -> { (index) : { (value):key} }
```

#### Output

```JSON
[ { "0": { "b": "a" } }, { "1": { "d": "c" } } ]
```

### Example

This example uses `pluck` to iterate over each element within `<prices/>` and returns arrays of
their keys, values, and indices. It uses anonymous parameters to capture them. Note that it uses
`as Number` to convert the values to numbers. Otherwise, they would return as strings.

#### Source

```DataWeave
%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($) as Number,
  "indices" : readXml.prices pluck($$$)
}
```

#### Output

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

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

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