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

# distinctBy

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

## distinctBy<T>(@StreamCapable items: Array<T>, criteria: (item: T, index: Number) -> Any): Array<T>

Iterates over the input and returns the unique elements in it.

DataWeave uses the result of the provided lambda as the uniqueness criteria.

This version of `distinctBy` finds unique values in an array. Other versions act on an object and
handle a `null` value.

### Parameters

<table><colgroup><col> <col></colgroup><thead><tr><th>Name</th><th>Description</th></tr></thead><tbody><tr><td><p><code>items</code></p></td><td><p>The array to evaluate.</p></td></tr><tr><td><p><code>criteria</code></p></td><td><p>The criteria used to select an <code>item</code> and/or <code>index</code> from the array.</p></td></tr></tbody></table>

### Example

This example inputs an array that contains duplicate numbers and returns an array with unique
numbers from that input. Note that you can write the same expression using an anonymous parameter
for the values: `[0, 1, 2, 3, 3, 2, 1, 4] distinctBy $`

#### Source

```DataWeave
%dw 2.0
output application/json
---
[0, 1, 2, 3, 3, 2, 1, 4] distinctBy (value) -> { "unique" : value }
```

#### Output

```JSON
[ 0, 1, 2, 3, 4]
```

### Example

This example removes duplicates of `"Kurt Cagle"` from an array.

#### Source

```DataWeave
%dw 2.0
output application/json
var record =  {
  "title": "XQuery Kick Start",
  "author": [
    "James McGovern",
    "Per Bothner",
    "Kurt Cagle",
    "James Linn",
    "Kurt Cagle",
    "Kurt Cagle",
    "Kurt Cagle",
    "Vaidyanathan Nagarajan"
  ],
  "year":"2000"
}
---
{
    "book" : {
      "title" : record.title,
      "year" : record.year,
      "authors" : record.author distinctBy $
    }
}
```

#### Output

```JSON
{
  "book": {
    "title": "XQuery Kick Start",
    "year": "2000",
    "authors": [
      "James McGovern",
      "Per Bothner",
      "Kurt Cagle",
      "James Linn",
      "Vaidyanathan Nagarajan"
    ]
  }
}
```

## distinctBy<K, V>(object: { (K)?: V }, criteria: (value: V, key: K) -> Any): Object

Removes duplicate key-value pairs from an object.

### 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 from which to remove the key-value pairs.</p></td></tr><tr><td><p><code>criteria</code></p></td><td><p>The <code>key</code> and/or <code>value</code> used to identify the key-value pairs to remove.</p></td></tr></tbody></table>

### Example

This example inputs an object that contains duplicate key-value pairs and returns an object with
key-value pairs from that input. Notice that the keys (`a` and `A`) are not treated with case
sensitivity, but the values (`b` and `B`) are. Also note that you can write the same expression
using an anonymous parameter for the values: `{a : "b", a : "b", A : "b", a : "B"} distinctBy $`

#### Source

```DataWeave
%dw 2.0
output application/json
---
{a : "b", a : "b", A : "b", a : "B"} distinctBy (value) -> { "unique" : value }
```

#### Output

```JSON
{ "a": "b", "a": "B" }
```

### Example

This example removes duplicates (`<author>James McGovern</author>`) from `<book/>`.

#### Source

```DataWeave
%dw 2.0
output application/xml
---
{
   book : {
     title : payload.book.title,
     authors: payload.book.&author distinctBy $
   }
}
```

#### Input

```XML
<book>
  <title> "XQuery Kick Start"</title>
  <author>James Linn</author>
  <author>Per Bothner</author>
  <author>James McGovern</author>
  <author>James McGovern</author>
  <author>James McGovern</author>
</book>
```

#### Output

```XML
<book>
  <title> "XQuery Kick Start"</title>
  <authors>
      <author>James Linn</author>
      <author>Per Bothner</author>
      <author>James McGovern</author>
  </authors>
</book>
```

## distinctBy(@StreamCapable items: Null, criteria: (item: Nothing, index: Nothing) -> Any): Null

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