cancel
Showing results for 
Search instead for 
Did you mean: 

How to render distinct elements only in an array with duplicates

mohamadelmardin
New Contributor III

I have a JSON document that has inside an array with duplicate values such as [1,1,3,3,6,1,2,1,2,100]. I am trying to transform/map it from source into target by returning only the distinct values such as [1,3,6,100]. How can I achieve that using a Mapper snap or if any other snap. For example when using jsonPath() function to specify a part of JSON document that has an array, is there a function similar to .unique() that could be attached to return only unique array elements?
The Unique snap works only on entire documents/rows not array elements within a document.

9 REPLIES 9

tstack
Former Employee

The expression language is a subset of JavaScript, so you can usually start by looking for JavaScript-based solutions.

In this case, take a look at this post:

The first answer has the simplest solution. The only change you have to make is to convert the callback function to use the arrow-function syntax, like so:

$arr.filter((item, pos, a) => a.indexOf(item) == pos)

Thank you. That was helpful :+1:

Thanks @tstack! For anyone wondering what this looks like in the Designer::

1075dfaa6bd3f171699fda212ee6c08382e56bd1.png

Pipeline .slp file: 20170404_mapper-distinct-elements_2017_04_04.slp (3.0 KB)

Anjali
New Contributor

Can you please explain how this filter function is working? what is item , pos and a here and how there are related to each other and functioning?