How to Use Set in JavaScript ES6 to Find Unique Items

Need unique values in JavaScript? That’s where the Set object comes in. Here’s how to use Set to filter a list of unique primitive values, objects by reference, or objects by their contents (values).

Image for post
Image for post
Photo by Stephen Leonardi on Unsplash

Unique JavaScript Values? Think Set

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

Image for post
Image for post
Photo by Andrew Tanglao on Unsplash

How Set Checks Value Equality

“A value in the Set may only occur once; it is unique in the Set's collection.” — MDN Docs

Image for post
Image for post
Photo by Josh Carter on Unsplash

Using Set for Unique Primitive Values in an Array

Image for post
Image for post
View raw code as a Github Gist
Image for post
Image for post
View raw code as a Github Gist
Image for post
Image for post
Photo by Tom Gainor on Unsplash

Unique Object References in an Array Using Set

Image for post
Image for post
View raw code as a GitHub Gist
Image for post
Image for post
Photo by Kevin Bree on Unsplash

How to Make an Array From a Set

Image for post
Image for post
View raw code as a GitHub Gist
Image for post
Image for post
Photo by Hugo Kruip on Unsplash

Iterating Through a Set of Unique Values

1 — Use Set.prototype.keys() or Set.prototype.values()

2 — Use Set.prototype.forEach() with a Callback Function

3 — Use a for...of Loop Directly With the New Set

Image for post
Image for post
View raw code as a GitHub Gist
Image for post
Image for post
Photo by Josh Carter on Unsplash

Unique Object Keys (Properties) or Values with Set

“JavaScript objects cannot have duplicate keys. The keys must all be unique.” — Jason Anders from TreeHouse

Image for post
Image for post
View raw code as GitHub Gist
Image for post
Image for post
Photo by Sandra Seitamaa on Unsplash

Using Set with an Array of Objects: Unique Values

Image for post
Image for post
View raw code as a GitHub Gist
Image for post
Image for post
Photo by Peter Scholten on Unsplash

Using Set with an Array of Objects: Unique Objects

Image for post
Image for post
View raw code as a GitHub Gist
Image for post
Image for post
View raw code as a GitHub Gist
Image for post
Image for post
Photo by NOAA on Unsplash

Finding the Unique Dates in an Array Using Set

Image for post
Image for post
View raw code as a GitHub Gist
Image for post
Image for post
Photo by Ronald Diel on Unsplash

Conclusion

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

Image for post
Image for post
Photo by Alex Azabache on Unsplash

Coding at Dawn

Laid-back programming + relaxing photography

Dr. Derek Austin 🥳

Written by

🤓 The physical therapist who writes JavaScript 💪 Web Developer 😎 Mentor 🧠 DPT 😄 SEO Expert 😁 React 😆 Jamstack 💬 Ask me anything 👉 DoctorDerek.com 👈

Coding at Dawn

Laid-back programming + relaxing photography

Dr. Derek Austin 🥳

Written by

🤓 The physical therapist who writes JavaScript 💪 Web Developer 😎 Mentor 🧠 DPT 😄 SEO Expert 😁 React 😆 Jamstack 💬 Ask me anything 👉 DoctorDerek.com 👈

Coding at Dawn

Laid-back programming + relaxing photography

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface.

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox.

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic.

Get the Medium app