Search code snippets, questions, articles...

Remove duplicate values from an array in Javascript

If you want to remove all duplicate values from a javascript array or want to get the unique values array then you can use the below code.
var my_array = [5, 4, 7, 8, 9, 2, 7, 5, 4, 8, 7];

// First Method
unique_array = my_array.filter((item, index, array) => array.indexOf(item) === index);
console.log("Unique Array is : " + unique_array);

// Second Method
var another_uni_arr = [ Set(my_array)];
console.log("Another Unique Array is : " + another_uni_arr);

In the first method, we are using Array.filter() method and checking if the item already exists in the array or not by using .indexOf() method.

Example Demo

Search Index Data (The code snippet can also be found with below search text)

Get unique values array in Javascript
Was this helpful?
Programming Feeds
Learn something new everyday on Devsheet