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: 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 5: 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 6: remove duplicates from array of objects javascript
arr.filter((v,i,a)=>a.findIndex(t=>(t.place === v.place && t.name===v.name))===i)