Nav

distinctBy

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

Returns only unique values from an array that may have duplicates. The lambda is invoked with two parameters: value and index. If these parameters are not defined, the index is defined by default as $$ and the value as $.

Transform

        
     
1
2
3
4
5
6
7
8
9
10
11
%dw 2.0
output application/json
---
{

    book : {
      title : payload.title,
      year: payload.year,
      authors: payload.author distinctBy $
    }
}
Input

        
     
1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
  "title": "XQuery Kick Start",
  "author": [
    "James McGovern",
    "Per Bothner",
    "Kurt Cagle",
    "James Linn",
    "Kurt Cagle",
    "Kurt Cagle",
    "Kurt Cagle",
    "Vaidyanathan Nagarajan"
  ],
  "year":"2000"
}
Output

        
     
1
2
3
4
5
6
7
8
9
10
11
12
13
{
  "book": {
    "title": "XQuery Kick Start",
    "year": "2000",
    "authors": [
      "James McGovern",
      "Per Bothner",
      "Kurt Cagle",
      "James Linn",
      "Vaidyanathan Nagarajan"
    ]
  }
}
distinctBy({ (K)?: V }, (value: V, key: K) → Any): Object

Returns an object with unlike key value pairs.

The function (a lambda) is invoked with two parameters: value and key. If these parameters are not defined, the index is defined by default as $$ and the value as $`.

Transform

        
     
1
2
3
4
5
6
7
8
9
10
%dw 2.0
output application/xml
---
{

     book : {
        title : payload.book.title,
        authors: payload.book.&author distinctBy $
     }
}
Input

        
     
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<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

        
     
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<book>
  <title> "XQuery Kick Start"</title>
  <authors>
      <author>
        James Linn
      </author>
      <author>
        Per Bothner
      </author>
      <author>
        James McGovern
      </author>
  </authors>
</book>

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.

+