removing duplicate objects in an array of objects using only javascript 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: 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 3: how to remove duplicate array object in javascript
let person = [
{name: "john"},
{name: "jane"},
{name: "imelda"},
{name: "john"},
{name: "jane"}
];
const obj = [...new Map(person.map(item => [JSON.stringify(item), item])).values()];
console.log(obj);