HomeTutorsContact

How to get all unique values in a JavaScript array?

By Gulshan Saini
Published in JavaScript
October 27, 2020
1 min read

Let’s say you have an array of numbers, which has duplicate items and, you want to clean it up so that it only holds unique value. To remove duplicate items, we can use the Set data structure. The Set object allows you to create a collection of unique values and does not permit the insertion of duplicate items.

Let’s look at the following example

The array variable holds a lot of duplicate items and only 11 unique items. To filter out duplicate items, we need to pass the array variable as input to the Set constructor.

That’s it, if we now log the value of the unique variable we can see it now holds unique values only.

Output

console.log(unique) // 1 3 4 5 6 7 8 9 0 11 12

Learn how to convert set to array again.


Tags

#javascript
Previous Article
How to Get Current Date in Human Readable Form in JavaScript?

Gulshan Saini

Fullstack Developer

Subscribe to our newsletter!

We'll send you the best of our blog just once a month. We promise.

Related Posts

How to convert Set to Array in JavaScript?

October 28, 2020
1 min
© 2020, All Rights Reserved.

Quick Links

Advertise with usContact Us

Social Media