removing and adding same object in array typescript code example
Example 1: how to remove duplicate array object in javascript
let days = ["senin","senin","selasa","selasa","rabu","kamis", "rabu"];
let fullname = [{name: "john"}, {name: "jane"}, {name: "imelda"}, {name: "john"},{name: "jane"}];
const arrOne = new Set(days);
console.log(arrOne);
const arrTwo = days.filter((item, index) => days.indexOf(item) == index);
console.log(arrTwo);
const arrObjOne = [...new Map(person.map(item => [JSON.stringify(item), item])).values()];
console.log(arrObjOne);
const arrObjTwo = Array.from(new Set(person.map(JSON.stringify))).map(JSON.parse);
console.log(arrObjTwo);
Example 2: 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'));