How do I remove all the duplicate values from an array and return all unique values in a JavaScript array?


I have a javascript array which may contain duplicates and I want to return only unique values. What is the simplest way to do this?

1 Answer

5 years ago by

Here is the simplest way to remove duplicates from an array in Javascript.

ES6 has a native object Set to store unique values and spread operator transforms the set back into an array.

let arr = ['x', 1, 2, 'x', 2, 1, 2];

let unique = [...new Set(arr)]; //results [ 'x', 1, 2 ]
5 years ago by Anusha