Categories
JavaScript Answers

How to get all unique values in a JavaScript array?

Spread the love

To get all unique values in a JavaScript array, we can convert it to a set and back.

For instance, we write

const myArray = ["a", 1, "a", 2, "1"];
const unique = [...new Set(myArray)];
console.log(unique);

to convert the myArray array to a set with the Set constructor.

Then we convert the set back to an array without duplicate values with the spread operator.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published.