Forum Discussion

mohamadelmardin's avatar
mohamadelmardin
New Contributor III
9 years ago

How to render distinct elements only in an array with duplicates

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

  • Hi @vcocco,

    If you think of getting the days between those dates, then you need to just switch the places of the dates.
    Math.floor((Date.now().minusDays(1)) - Date.parse("2021-08-01") / (1000*60*60*24))

    But if you want to get range of dates then, use this expression.
    sl.range(0, Math.floor((Date.now().minusDays(1)) - Date.parse("2021-08-01") / (1000*60*60*24))).map(x => Date.parse("2021-08-01").plusDays(x))

    This will return you array with range of dates, so after the mapper you can use JSON Splitter snap to split the dates in more documents.

    Regards,
    Viktor Nedanovski