remove duplicate objects from an array code example
Example 1: how to remove duplicate array object in javascript
let person = [{name: "john"}, {name: "jane"}, {name: "imelda"}, {name: "john"}];
function removeDuplicates(data, key) {
return [
...new Map(data.map(item => [key(item), item])).values()
]
};
console.log(removeDuplicates(person, item => item.name));
Example 2: how to remove duplicate array object in javascript
let person = [
{name: "john"},
{name: "jane"},
{name: "imelda"},
{name: "john"},
{name: "jane"}
];
const data = Array.from(new Set(person.map(JSON.stringify))).map(JSON.parse);
console.log(data);
Example 3: remove duplicate objects from array javascript
const addresses = [...];
const uniqueAddresses = Array.from(new Set(addresses.map(a => a.id)))
.map(id => {
return addresses.find(a => a.id === id)
})
Example 4: remove duplicates from array of objects javascript
arr.filter((v,i,a)=>a.findIndex(t=>(t.place === v.place && t.name===v.name))===i)
Example 5: remove duplicates objects from array javascript
arr.reduce((acc, current) => {
const x = acc.find(item => item.id === current.id);
if (!x) {
return acc.concat([current]);
} else {
return acc;
}
}, []);