remove duplicate objects based on id from array angular 8 code example
Example 1: remove duplicate objects based on id from array angular 8
function getUnique(arr, comp) {
const unique = arr.map(e => e[comp])
.map((e, i, final) => final.indexOf(e) === i && i)
.filter((e) => arr[e]).map(e => arr[e]);
return unique;
}
console.log(getUnique(arr,'id'));
Example 2: how to remove duplicate array object in javascript
var arrayWithDuplicates = [
{"type":"LICENSE", "licenseNum": "12345", state:"NV"},
{"type":"LICENSE", "licenseNum": "A7846", state:"CA"},
{"type":"LICENSE", "licenseNum": "12345", state:"OR"},
{"type":"LICENSE", "licenseNum": "10849", state:"CA"},
{"type":"LICENSE", "licenseNum": "B7037", state:"WA"},
{"type":"LICENSE", "licenseNum": "12345", state:"NM"}
];
function removeDuplicates(originalArray, prop) {
var newArray = [];
var lookupObject = {};
for(var i in originalArray) {
lookupObject[originalArray[i][prop]] = originalArray[i];
}
for(i in lookupObject) {
newArray.push(lookupObject[i]);
}
return newArray;
}
var uniqueArray = removeDuplicates(arrayWithDuplicates, "licenseNum");
console.log("uniqueArray is: " + JSON.stringify(uniqueArray));
Example 3: remove duplicates from array of objects javascript
arr.filter((v,i,a)=>a.findIndex(t=>(t.place === v.place && t.name===v.name))===i)