array js remove duplicates code example
Example 1: js delete duplicates from array
const names = ['John', 'Paul', 'George', 'Ringo', 'John'];
let unique = [...new Set(names)];
console.log(unique);
Example 2: javascript remove duplicates from array
unique = [...new Set(arr)];
Example 3: js delete duplicates from array
const names = ['John', 'Paul', 'George', 'Ringo', 'John'];
function removeDups(names) {
let unique = {};
names.forEach(function(i) {
if(!unique[i]) {
unique[i] = true;
}
});
return Object.keys(unique);
}
removeDups(names);
Example 4: javascript remove duplicate strings from array
let uniqueArray = [...new Set(arrayWithDuplicates)];
function removeArrayDuplicates(arrayWithDuplicates) {
let seen = {};
let uniqueArray = [];
let len = arrayWithDuplicates.length;
let j = 0;
for(let i = 0; i < len; i++) {
let item = arrayWithDuplicates[i];
if(seen[item] !== 1) {
seen[item] = 1;
uniqueArray[j++] = item;
}
}
return uniqueArray;
}
Example 5: remove duplicates from array javascript
arr.filter((v,i,a)=>a.findIndex(t=>(t.place === v.place && t.name===v.name))===i)