How to remove all duplicates from an array of objects?
Zero in on duplicates in an array of objects using filter()
together with findIndex()
. This one-liner retains only the first unique copy of an object based on a designated key. In the following example, we keep or discard objects based on their id
:
Deep object comparison
Let's crank up the heat a bit. Complex objects may call for JSON.stringify()
, for a more deep-seated comparison. Be mindful, performance nerds, large datasets might suffer a screeching slowdown:
Caution: Property order matters when using JSON.stringify()
. If you've got fluctuating property orders, better saddle up with more intricate comparison function.
Building helpful utilities
For detecting duplicates, bag together your logic using isPropValuesEqual
, a utility function that makes checking certain properties across objects as easy as Sunday morning:
Likewise, embrace the might of getUniqueItemsByProperties
, designed to dynamically weed out duplicates according to multiple properties:
Taking advantage of ES6+ and Map
The release of ES6 introduced Map
, great friends for those in dire need of unique key-value pairs:
By populating new map with key-value pairs (object key = 'stringified' object), then turning the .values()
iterator back into an array, you cast an effective spell to annihilate duplicates.
Complex keys and speedy performance
When unique identifiers go M.I.A., create compound keys by knocking together property values with function like objToId
:
Note of caution to those upholding original element order - Map
-based methods might leave you high and dry. Avoid JSON.stringify()
for large data sets due to performance cost. Check out external libraries equipped for complex object structures to avoid splitting hairs.
Bonus: Modern techniques for uniqueness
Remember to use Set
although it doesn't play well with unique object references. Here's how you can make it work with array spreading and comparator functions:
For state-of-the-art multi-property uniqueness, leverage every()
within filter()
. Preserving last instance of duplicates? Swap findIndex
with a tailor-made findLastIndex
function.
Was this article helpful?