how to remove duplicate objects from an array of objects in 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 duplicates objects from array javascript
function remove_duplicate_objects(data,prop) {
var seen = {};
data = data.filter(function (entry) {
if (seen.hasOwnProperty(entry[prop])) {
return false;
}
seen[entry.prop] = entry;
return true;
});
return data
}
const new_array = remove_duplicate_objects([array with objects inside])