remove repeated id object from array javascript code example
Example 1: Removing duplicates in an Array of Objects
const seen = new Set();
const arr = [
{ id: 1, name: "test1" },
{ id: 2, name: "test2" },
{ id: 2, name: "test3" },
{ id: 3, name: "test4" },
{ id: 4, name: "test5" },
{ id: 5, name: "test6" },
{ id: 5, name: "test7" },
{ id: 6, name: "test8" }
];
const filteredArr = arr.filter(el => {
const duplicate = seen.has(el.id);
seen.add(el.id);
return !duplicate;
});
Example 2: remove duplicates from array of objects javascript
arr.filter((v,i,a)=>a.findIndex(t=>(t.place === v.place && t.name===v.name))===i)