Code Snippets - Unique array in JavaScript

Code Snippets - Unique array in JavaScriptPhoto by
By Hemerson Carlin
1 min read

This is another blog post I write about code snippets I find useful (you can check the other blog posts and ). And today's code snippet is about how to get unique values from an array in JavaScript.

I have seen some versions of this snippet before, but the one I like to recommend the most is the use of a object alongside with the in JavaScript.

By default, a Set is meant to store unique values and spreading it into an array is enough to give us the desired result.

const uniqueArray = (arr) => [...new Set(arr)]

uniqueArray(['ReactJS', 'NextJS', 'NodeJS', 'ReactJS']) // ['ReactJS', 'NextJS', 'NodeJS']

uniqueArray([1, 2, 2, 3, 3, 3, 4, 4, 4, 4]) // [1, 2, 3, 4]

I hope that's helpful and interesting to you. 👋🏼

Did you know you can help me with this page?

If you see something wrong, think this page needs clarification, you found a typo or any other suggestion you might have feel free to open a PR and I will take care of the rest.

My entire site is available to edit on GitHub and all are very welcome 🤙🏼.

mersocarlin

Hemerson Carlin, also known as mersocarlin, is passionate and resourceful full-stack Software Engineer with 10+ years of experience focused on agile development, architecture and team building.

This is the space to share the things he likes, a couple of ideas and some of his work.

Previous Blog Posts