DEV Community

Discussion on: 3 ways to remove duplicates in an Array in Javascript

Collapse
 
vedranbasic profile image
Vedran-Basic

Imagine having array of objects with n props having only to remove duplicate props with m same properties where m < n and use first of the two or more duplicates of the same unique constraint rule.

That's where science begins. Would be much more interested in hearing different solutions to this topic.

Collapse
 
monello profile image
Morné Louw

Then write your own article an stop trying to sound smart on someone else's post

Collapse
 
phibya profile image
Phi Byă • Edited

Simply use reduce and Map:

[{a: 1, b: 2, c: 3}, {a: 2, b: 3, c: 4}, {a: 1, b: 2, c: 5}].reduce((p, c) => p.set([c.a, c.b].join('|'), c), new Map()).values()

Edit: For selecting the first value, use Map.has before setting the value.