xxxxxxxxxx
[new Set(['1','1','2','2','3','3'])]
//this will return a new array with the unique primitive values
//I guess doing this with objects will require implementing equality method.
xxxxxxxxxx
let arr = [1,2,3,1,1,1,4,5]
let filtered = arr.filter((item,index) => arr.indexOf(item) === index)
console.log(filtered) // [1,2,3,4,5]
xxxxxxxxxx
// how to remove duplicates from array in javascript
// 1. filter()
let num = [1, 2, 3, 3, 4, 4, 5, 5, 6];
let filtered = num.filter((a, b) => num.indexOf(a) === b)
console.log(filtered);
// Result: [ 1, 2, 3, 4, 5, 6 ]
// 2. Set()
const removeDuplicates = (arr) => [new Set(arr)];
console.log(removeDuplicates([1, 2, 3, 3, 4, 4, 5, 5, 6]));
// Result: [ 1, 2, 3, 4, 5, 6 ]
xxxxxxxxxx
const myArray = [2,5,6,2,2,4,5,3,3];
const uniqueArray = [new Set(myArray)];
console.log(uniqueArray);
xxxxxxxxxx
// for TypeScript and JavaScript
const initialArray = ['a', 'a', 'b',]
finalArray = Array.from(new Set(initialArray)); // a, b
xxxxxxxxxx
uniqueArray = a.filter(function(item, pos) {
return a.indexOf(item) == pos;
})
xxxxxxxxxx
var myArr = [1, 2, 2, 2, 3];
var mySet = new Set(myArr);
myArr = [mySet];
console.log(myArr);
// 1, 2, 3
xxxxxxxxxx
const removeDuplicates = (arr) => [new Set(arr)];
const arr = [1, 2, 3, 4, 5, 3, 1, 2, 5];
const distinct = removeDuplicates(arr);
console.log(distinct); // [1, 2, 3, 4, 5]
xxxxxxxxxx
const array = [1, 1, 2, 3, 5, 5, 1]
const uniqueArray = [new Set(array)];
console.log(uniqueArray); // Output: [1, 2, 3, 5]
xxxxxxxxxx
uniqueArray = a.filter(function(item, pos) {
return a.indexOf(item) == pos;
})