DEV Community

loading...

Remove duplicates from array in javascript

Uifoxes
・1 min read

You can use filter() method to remove duplicates from array , if the index of the element gets matched with the indexOf value then only that element will get added.

let data = [1, 2, 3, 2, 1];

let uniqueData = data.filter((c, index) => {
return data.indexOf(c) == index;
});

console.log(uniqueData);

Output - [ 1, 2, 3 ]

Source : https://codelearningpoint.com/

Discussion (1)

Collapse
captainyossarian profile image
yossarian • Edited
const result = [...new Set([1,1,2,3])] // [1,2,3]
Enter fullscreen mode Exit fullscreen mode