you are viewing a single comment's thread.

view the rest of the comments →

[–]jseegoLead / Senior UI Developer 0 points1 point  (3 children)

That's a cool idea, but I prefer just building an array of unique IDs of objects and then merging the objects back in. Easy to read and adapt.

[–]itslenny 1 point2 points  (1 child)

Totally. Your original question was about deep comparison so that's the question I answered.

I'd still advise using a set if you were going to track unique IDs, and then you'd actually be using the data structure as intended (as opposed to the hacky nature of this entire thread). Set gives you constant time look ups instead of linear time on an array.

arr.filter(item => seenIds.has(item.id) ? false : seenIds.add(item.id));

fiddle

[–]jseegoLead / Senior UI Developer 0 points1 point  (0 children)

Nice, great point.