best way to remove duplicates from array code example
Example 1: removing duplicates in array javascript
let arr = [1,2,3,1,1,1,4,5]
let filtered = arr.filter((item,index) => arr.indexOf(item) === index)
console.log(filtered)
Example 2: remove duplicates from array
let chars = ['A', 'B', 'A', 'C', 'B'];
let uniqueChars = [...new Set(chars)];
console.log(uniqueChars);
output:
[ 'A', 'B', 'C' ]
Example 3: remove duplicates from array javascript
function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}
var a = ['a', 1, 'a', 2, '1'];
var unique = a.filter(onlyUnique);
console.log(unique);