cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Counting Instances in Array Object and Removing Duplicates

alex_panganiban
Contributor

As I process an array, I want to see if I have any duplicate names in the array that I am processing. If I encounter a duplicate name, I only want to keep the one where the series contains โ€œTrekker.โ€ How can I do this?

For example, given the shows array below. My goal is to remove the first instance of Spock and keep the instance that contains the word, Trekker.

shows: [
{ name: โ€˜James T. Kirkโ€™, series: โ€˜Star Trekโ€™ },
{ name: โ€˜Spockโ€™, series: โ€˜Star Trekโ€™, โ€˜Star Trek: The Next Generationโ€™ },
{ name: โ€˜Jean-Luc Picardโ€™, series: โ€˜Star Trek: The Next Generationโ€™ },
{ name: โ€˜Worfโ€™, series: โ€˜Star Trek: The Next Generationโ€™ },
{ name: โ€˜Spockโ€™, series: โ€˜Star Trekโ€™, โ€˜Star Trekkerโ€™ }
]

Iโ€™ve been able to get rid of duplicate names using the following filter method, however, due to the positioning index, it always gets rid of the one I want to keep.

$shows.filter((show, pos, a) => a.findIndex(item => item.name == a[pos].name) == pos)

In this example, I got all 5 instances of the array back.

$shows.filter((show, pos, a) => a.findIndex(item => item.name == a[pos].name) != pos
&& a[pos].series.contains(โ€œTrekkerโ€)
|| a.findIndex(item => item.name == a[pos].name) == pos)

I thought if I could get the count of the current name, then I could leverage the count value to incorporate in the filters above, however, I canโ€™t figure out how to do that.

$shows.filter(show => show.name == โ€œSpockโ€).length

$shows.filter((show, pos, a) => a.findIndex(item => item.name == a[pos].name) != pos
&& a[pos].series.contains(โ€œTrekkerโ€)
|| a.findIndex(item => item.name == a[pos].name) == pos && COUNT == 1)

I also tried using the array.prototype.findLastIndex, but either SnapLogic didnโ€™t like it or maybe itโ€™s a Java/Javascript version issue.

Please help. My Javascript skills, especially when using callbacks, sucks. And thank you.

1 ACCEPTED SOLUTION

Supratim
Contributor III

@alex.panganiban.guild use attached pipeline.
removeDuplicateEntry_2022_07_25.slp (5.3 KB)

View solution in original post

3 REPLIES 3

Supratim
Contributor III

@alex.panganiban.guild use attached pipeline.
removeDuplicateEntry_2022_07_25.slp (5.3 KB)

OMGosh, Supratim, this is an amazing solution! Thank you so much! The nested callbacks and using the merge function was BRILLIANT! I would never have been able to come up with this on my own. I really learned something special from you. People like you make this Community page so great! Thanks again!

By the way, do you know anything about not being to use the findLastIndex() function?

@alex.panganiban.guild Thanks a lot for your appreciation. To be frank I didnโ€™t explore much on fundLastIndex() function in array/