To remove duplicates from an array in JavaScript, you can use various methods such as using a Set or filter function.
In this example,
let numbers = [1, 2, 2, 3, 4, 4, 5];
let uniqueNumbers = [...new Set(numbers)];
console.log('Array without duplicates:', uniqueNumbers);
Array without duplicates: [ 1, 2, 3, 4, 5 ]
In this example,
let colors = ['red', 'green', 'blue', 'green', 'yellow', 'blue'];
let uniqueColors = colors.filter((value, index, self) => self.indexOf(value) === index);
console.log('Array without duplicates:', uniqueColors);
Array without duplicates: [ 'red', 'green', 'blue', 'yellow' ]
In this tutorial, we learned How to Remove Duplicates in an Array in JavaScript language with well detailed examples.