How to Find Unique Objects in an Array in JavaScript by Object Reference or Key-Value Pairs

I’ll use Set to remove duplicate objects from an array by object reference. Then I’ll find objects by looking at key-value pairs.

Pestcampfighwad
8 min readDec 19, 2020
Photo by Joe Green on Unsplash

Finding Unique Objects in a JavaScript Array

The powerful ES6 feature Set only accepts one copy of each value added to it, making it perfect for finding distinct objects in JavaScript.

“The Set object lets you store unique values of any type, whether primitive values or object references.”

— MDN docs

In this tutorial, you’ll learn exactly how best to use Set when working with an array containing JavaScript objects that may be duplicates.

I’ll be exploring using Set to remove any duplicate objects from the array. First, I’ll find unique objects based on the objects’ variable name (objects with the same object reference). Then, I’ll filter for objects with identical key-value pairs (that have the same content but different object references).

http://vidrio.org/wnc/videos-Parma-Juventus-v-en-gb-1afc-10.php
http://vidrio.org/wnc/videos-Parma-Juventus-v-en-gb-1khs-17.php
http://actiup.com/ggv/v-ideos-RS-Berkane-Raja-Club-Athletic-v-en-gb-1zfx-4.php
http://vidrio.org/wnc/v-ideos-Parma-Juventus-v-en-gb-1wlf-8.php
http://vidrio.org/wnc/videos-Genk-KV-Kortrijk-v-en-gb-1qmh-8.php
https://kleinoot.nl/tgo/Video-SV-Zulte-Waregem-STVV-v-en-gb-1gqt-.php
http://vidrio.org/wnc/Video-Genk-KV-Kortrijk-v-en-gb-1gpb-1.php
http://vidrio.org/wnc/videos-Genk-KV-Kortrijk-v-en-gb-1zur-7.php
http://actiup.com/ggv/Video-RS-Berkane-Raja-Club-Athletic-v-en-gb-1vmf-14.php
http://actiup.com/ggv/v-ideos-Gaz-Metan-Medias-Viitorul-Constanța-v-en-gb-1ara-14.php
http://actiup.com/ggv/Video-Gaz-Metan-Medias-Viitorul-Constanța-v-en-gb-1odw-1.php
http://actiup.com/ggv/video-Gaz-Metan-Medias-Viitorul-Constanța-v-en-gb-1brc-15.php
http://vidrio.org/wnc/videos-MC-Alger-Buffles-de-Borgou-v-en-gb-inh-.php
http://vidrio.org/wnc/Video-MC-Alger-Buffles-de-Borgou-v-en-gb-drk-.php
http://vidrio.org/wnc/video-MC-Alger-Buffles-de-Borgou-v-en-gb-ghf-.php
https://kleinoot.nl/tgo/videos-SV-Zulte-Waregem-STVV-v-en-gb-1drb-7.php
http://actiup.com/kck/Video-ferencvarosi-v-budafoki-v-hu-hu-1dfz-10.php
http://actiup.com/kck/video-ferencvarosi-v-budafoki-v-hu-hu-1zxm-1.php
https://kleinoot.nl/tgo/v-ideos-SV-Zulte-Waregem-STVV-v-en-gb-1sxm-11.php
http://actiup.com/kck/videos-ferencvarosi-v-budafoki-v-hu-hu-1dce-8.php
https://kleinoot.nl/tgo/v-ideos-Osasuna-Villarreal-v-en-gb-1iqd-.php
http://vidrio.org/wnc/videos-Real-Zaragoza-Lugo-v-en-gb-cky-.php
https://kleinoot.nl/tgo/Video-Osasuna-Villarreal-v-en-gb-1ztu-5.php
http://vidrio.org/wnc/videos-Real-Zaragoza-Lugo-v-en-gb-not-.php
https://kleinoot.nl/tgo/v-ideos-Osasuna-Villarreal-v-en-gb-1fha-6.php
http://vidrio.org/wnc/video-Real-Zaragoza-Lugo-v-en-gb-guc-.php
http://vidrio.org/wnc/videos-Newcastle-United-Fulham-v-en-gb-mfg-.php
http://vidrio.org/wnc/video-Newcastle-United-Fulham-v-en-gb-ect-.php
https://kleinoot.nl/tgo/v-ideos-bayer-leverkusen-v-bayern-munchen-v-de-de-1jlb-9.php
http://vidrio.org/wnc/videos-Newcastle-United-Fulham-v-en-gb-tis-.php
https://kleinoot.nl/tgo/Video-bayer-leverkusen-v-bayern-munchen-v-de-de-1tyl-8.php
http://vidrio.org/wnc/Video-Newcastle-Fulham-v-en-gb-bli-.php
https://kleinoot.nl/tgo/Video-bayer-leverkusen-v-bayern-munchen-v-de-de-1wro-12.php
http://vidrio.org/wnc/videos-Newcastle-Fulham-v-en-gb-anu-.php
http://vidrio.org/wnc/Video-Newcastle-Fulham-v-en-gb-pyx-.php
http://vidrio.org/wnc/videos-real-zaragoza-v-lugo-v-es-es-1gko-4.php
http://vidrio.org/wnc/Video-real-zaragoza-v-lugo-v-es-es-1loy-14.php
http://vidrio.org/wnc/videos-real-zaragoza-v-lugo-v-es-es-1nfw-3.php
http://vidrio.org/wnc/Video-real-zaragoza-v-lugo-v-es-es-1rwk2-9.php
http://vidrio.org/wnc/Video-real-zaragoza-v-lugo-v-es-es-1scm2-15.php
http://vidrio.org/wnc/Video-FC-Utrecht-Fortuna-Sittard-v-en-gb-1oth-15.php
https://kleinoot.nl/vxr/Video-OFI-Crete-Aris-Thessaloniki-v-gr-gr-1poy-12.php
http://vidrio.org/wnc/videos-FC-Utrecht-Fortuna-Sittard-v-en-gb-1veu-9.php
https://kleinoot.nl/vxr/videos-OFI-Crete-Aris-Thessaloniki-v-gr-gr-1jyf-16.php
http://vidrio.org/wnc/v-ideos-FC-Utrecht-Fortuna-Sittard-v-en-gb-1sui-8.php
https://kleinoot.nl/vxr/v-ideos-OFI-Crete-Aris-Thessaloniki-v-gr-gr-1igx-9.php
https://kleinoot.nl/vxr/video-OFI-Crete-Aris-Thessaloniki-v-gr-gr-1cdw-9.php
https://kleinoot.nl/vxr/video-OFI-Crete-Aris-Thessaloniki-v-gr-gr-1xmi-19.php
https://kleinoot.nl/vxr/Video-Levante-Real-Sociedad-v-en-gb-1hkn-.php
http://actiup.com/kck/videos-ferencvarosi-v-budafoki-v-hu-hu-1mll-17.php
https://kleinoot.nl/vxr/Video-Levante-Real-Sociedad-v-en-gb-1erz-7.php
http://vidrio.org/wnc/video-santa-cruz-v-vila-nova-v-pt-br-1zfx2-6.php
https://kleinoot.nl/vxr/video-Levante-Real-Sociedad-v-en-gb-1jps-9.php
http://vidrio.org/wnc/v-ideos-santa-cruz-v-vila-nova-v-pt-br-1vci-8.php
http://actiup.com/kck/video-Ferencvarosi-Budafoki-MTE-v-en-gb-ccd-.php
http://actiup.com/kck/v-ideos-Ferencvarosi-Budafoki-MTE-v-en-gb-zld-.php
http://vidrio.org/wnc/video-santa-cruz-v-vila-nova-v-pt-br-1jmy2-14.php
https://kleinoot.nl/vxr/video-Everton-Arsenal-v-en-gb-1iwe-.php
http://actiup.com/kck/videos-Ferencvarosi-Budafoki-MTE-v-en-gb-zvr-.php
http://vidrio.org/wnc/video-santa-cruz-v-vila-nova-v-pt-br-1zjo2-10.php
https://kleinoot.nl/vxr/Video-Everton-Arsenal-v-en-gb-1kae-16.php
http://actiup.com/kck/video-Sccm-Chabab-Mohammedia-Rapide-Oued-Zem-v-en-gb-tpx-.php
http://actiup.com/kck/videos-Sccm-Chabab-Mohammedia-Rapide-Oued-Zem-v-en-gb-qyo-.php
http://vidrio.org/wnc/v-ideos-santa-cruz-v-vila-nova-v-pt-br-1jix-12.php
https://kleinoot.nl/vxr/Video-Everton-Arsenal-v-en-gb-1eos-16.php
http://vidrio.org/wnc/v-ideos-OGC-Nice-Olympique-Lyon-v-en-gb-1hul-12.php
https://kleinoot.nl/vxr/videos-Everton-Arsenal-v-en-gb-1iyf-.php
https://kleinoot.nl/vxr/video-Everton-Arsenal-v-en-gb-1gvs-12.php
http://actiup.com/kck/v-ideos-Sccm-Chabab-Mohammedia-Rapide-Oued-Zem-v-en-gb-mpw-.php
http://vidrio.org/wnc/video-OGC-Nice-Olympique-Lyon-v-en-gb-1wwx-8.php
http://vidrio.org/wnc/Video-OGC-Nice-Olympique-Lyon-v-en-gb-1psm-15.php
https://kleinoot.nl/vxr/Video-Everton-Arsenal-v-en-gb-1tku-4.php
http://actiup.com/kck/v-ideos-lech-poznan-v-wisla-krakow-v-pl-pl-1ksr-2.php
http://actiup.com/kck/Video-lech-poznan-v-wisla-krakow-v-pl-pl-1fqg-7.php
https://kleinoot.nl/vxr/videos-levante-v-real-sociedad-v-es-es-1xwy-4.php
http://vidrio.org/wnc/v-ideos-nice-v-olympique-lyonnais-v-fr-fr-1ban-4.php
http://actiup.com/kck/video-lech-poznan-v-wisla-krakow-v-pl-pl-1prg-17.php
http://actiup.com/kck/video-lech-poznan-v-wisla-krakow-v-pl-pl-1jil-6.php
http://vidrio.org/wnc/v-ideos-nice-v-olympique-lyonnais-v-fr-fr-1qgl-5.php
https://kleinoot.nl/vxr/Video-levante-v-real-sociedad-v-es-es-1ivd3-2.php
https://kleinoot.nl/vxr/Video-levante-v-real-sociedad-v-es-es-1ayx2-13.php
http://vidrio.org/wnc/video-nice-v-olympique-lyonnais-v-fr-fr-1shc-1.php
http://actiup.com/kck/v-ideos-lech-poznan-v-wisla-krakow-v-pl-pl-1omx-2.php
http://vidrio.org/wnc/video-nice-v-olympique-lyonnais-v-fr-fr-1eta-8.php
https://kleinoot.nl/vxr/videos-levante-v-real-sociedad-v-es-es-1nud2-5.php
https://kleinoot.nl/vxr/video-levante-v-real-sociedad-v-es-es-1dhq-13.php
http://vidrio.org/wnc/Video-nice-v-olympique-lyonnais-v-fr-fr-1dwa-8.php
https://kleinoot.nl/vxr/video-OFI-Crete-Aris-Thessaloniki-v-en-gb-1sqc-.php
http://vidrio.org/wnc/Video-danubio-v-penarol-v-es-ur-1vum2-4.php
http://vidrio.org/wnc/Video-danubio-v-penarol-v-es-ur-1stf2-12.php
https://kleinoot.nl/vxr/video-OFI-Crete-Aris-Thessaloniki-v-en-gb-1dal-11.php
http://actiup.com/kck/videos-marsel-v-reims-v-yt2-1efo-8.php
http://vidrio.org/wnc/videos-danubio-v-penarol-v-es-ur-1pkt2-9.php
https://kleinoot.nl/vxr/videos-OFI-Crete-Aris-Thessaloniki-v-en-gb-1ysn-8.php
http://actiup.com/kck/videos-marsel-v-reims-v-yt2-1oih-13.php
http://vidrio.org/wnc/Video-danubio-v-penarol-v-es-ur-1amr-6.php
http://actiup.com/kck/video-marsel-v-reims-v-yt2-1dph-14.php
http://actiup.com/kck/video-marsel-v-reims-v-yt2-1qve-2.php
http://vidrio.org/wnc/Video-danubio-v-penarol-v-es-ur-1kob-2.php
http://actiup.com/kck/Video-marsel-v-reims-v-yt2-1ngc-8.php

Using Set to Find Unique Objects

You may already be familiar with the basic idea of Set — you can only add each value to a Set once, leaving a list of only the unique items.

However, Set works differently with primitive values compared to when working with JavaScript objects.

That’s because JavaScript objects, even those with the exact same contents, have different object references, also known as pointers in memory.

Set is still very useful for removing duplicate objects from an array since it’s possible to add an object stored in a variable multiple times to an array. In that case, where the same exact object has been added multiple times, Set will help you remove the nonunique object references, leaving just one copy.

However, you can also filter an array of objects while comparing their contents using Set. In doing so, you can find the distinct objects by removing any that have duplicate key-value pairs. I refer to this as finding unique objects by content, where content means key-value pairs (also called entries).

Find Unique JavaScript Objects by Object Reference

Every variable in JavaScript lives in some portion of the computer’s memory, whether that computer is a server (running Node.js) or a web browser.

When you define a variable with a given name that name becomes associated with a place in memory. For objects stored as variables, these places in memory are called object references— and each object has a unique one.

You’re nevertheless able to add that unique object reference multiple times to a given array. That’s probably not the behavior you want in your code, so here’s how to remove duplicate object references from an array.

View raw code as a GitHub gist

Using Set makes solving this problem much simpler than needing to loop through the array yourself, comparing every object.

Find Unique Objects by Content (Key-Value Pairs)

Since Set wants to compare object references by default, we need to convert our objects into a format that Set will be able to compare.

To extract the objects’ contents for comparison, you can take advantage of JSON.stringify() and JSON.parse(). These great methods turn JavaScript objects into JSON format (JavaScript object notation) strings.

Set works great for finding unique strings in an array so we’ll be able to convert the unique JSON strings back into JavaScript objects.

View raw code as a GitHub gist

You do want to be aware here of how JSON handles converting data because certain values are not JSON-safe.

“undefined, Functions, and Symbols are not valid JSON values […]

Date [objects] are treated as strings.

The numbers Infinity and NaN, as well as the value null, are all considered null.”

— MDN docs

For primitive types like numbers, strings, and boolean values, though, you’ll find this method of using Set with JSON.stringify and JSON.parse works great for finding distinct objects by their contents, even for nested objects.

Conclusion: Finding Distinct JavaScript Objects

When you have an array of objects in JavaScript, you usually want to work with the unique objects — you don’t want any duplicate objects that could cause bugs or a poor user experience.

However, JavaScript has two definitions of what makes a unique object:

An object’s reference (its variable name or location in memory)

An object’s contents (its key-value pairs or entries)

Objects with unique references are different objects in memory, even if they have the exact same contents, like a deep clone of a JavaScript object. To find objects with unique references, you need to make a Set from the array. Comparing objects by reference is the default behavior of Set.

To get Set to compare objects by their contents, you’ll need to take advantage of JSON.stringify() and JSON.parse() in order to turn the objects into string primitives in JSON format that can be compared by Set. If your objects are JSON-safe, then this will work great, even for nested objects. Pretty sweet!

Hopefully this article has helped you understand the difference between object references versus the contents of those objects when trying to find the distinct objects in a JavaScript array.

To learn more about Set, please refer to my complete guide to using Set in JavaScript.

Happy coding! 🎄🎁🎅🥂🎇

Join my email list to get free access to all of my Medium articles.

--

--

Pestcampfighwad
0 Followers

Writing mostly to myself. Sharing some of it with you. Hope it helps.