%dw 2.0
output application/json
---
[0, 1, 2, 3, 3, 2, 1, 4] distinctBy (value) -> { "unique" : value }
distinctBy
DataWeave 2.2 is compatible and bundled with Mule 4.2. This version of Mule reached its End of Life on May 2, 2023, when Extended Support ended. 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>
Iterates over an array and returns the unique elements in it. DataWeave uses the result of applying 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
Name | Description |
---|---|
|
The array to evaluate. |
|
The criteria used to select an |
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 $
Example
This example removes duplicates of "Kurt Cagle"
from an 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 $
}
}
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 |
---|---|
|
The object from which to remove the key-value pairs. |
|
The |
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 $
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 $
}
}