Categories
JavaScript Answers

How to sort array and get unique entries with JavaScript?

Spread the love

To sort array and get unique entries with JavaScript, we use a set.

For instance, we write

const myData = ["237", "124", "255", "124", "366", "255"];
const uniqueAndSorted = [...new Set(myData)].sort();

to convert the myData array to a set with the Set constructor to remove the duplicates.

Then we spread the set elements into an array with the spread operator.

Finally, we sort the array without the duplicates with sort.

By John Au-Yeung

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

Leave a Reply

Your email address will not be published. Required fields are marked *