Contact Us 1-800-596-4880

distinctBy

DataWeave 2.1 is compatible with Mule 4.1. Standard Support for Mule 4.1 ended on November 2, 2020, and this version of Mule will reach its End of Life on November 2, 2022, when Extended Support ends.

Deployments of new applications to CloudHub that use this version of Mule are no longer allowed. Only in-place updates to applications are permitted.

MuleSoft recommends that you upgrade to the latest version of Mule 4 that is in Standard Support so that your applications run with the latest fixes and security enhancements.

distinctBy(Array<T>, (item: T, index: Number) -> Any): Array<T>

Returns unique values from a list (array) that might have duplicates. DataWeave uses the result of applying the provided lambda as the uniqueness criteria.

Parameters

Name Description

items

The list (Array type).

criteria

The item and/or index used to return.

Example

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

Source

%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

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

distinctBy(Null, (item: Nothing, index: Nothing) -> Any): Null

Helper function that allows distinctBy to work with null values.

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

Removes duplicate key-value pairs from an Object.

Parameters

Name Description

object

The object from which to remove the key-value pairs.

criteria

The key and/or value used to identify the key-value pairs to remove.

Example

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

Source

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

Input

<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

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