How to Remove All Duplicates from an Array of Objects

How to remove all duplicates from an array of objects?

A primitive method would be:

const obj = {};

for (let i = 0, len = things.thing.length; i < len; i++) {
obj[things.thing[i]['place']] = things.thing[i];
}

things.thing = new Array();

for (const key in obj) {
things.thing.push(obj[key]);
}

Remove duplicate values from an array of objects in javascript

You can use array#reduce and array#some.

const arr = [

{label: 'All', value: 'All'},

{label: 'All', value: 'All'},

{label: 'Alex', value: 'Ninja'},

{label: 'Bill', value: 'Op'},

{label: 'Cill', value: 'iopop'}

]

var result = arr.reduce((unique, o) => {

if(!unique.some(obj => obj.label === o.label && obj.value === o.value)) {

unique.push(o);

}

return unique;

},[]);

console.log(result);

Removing duplicate objects (based on multiple keys) from array

You could use a Set in a closure for filtering.

const

listOfTags = [{ id: 1, label: "Hello", color: "red", sorting: 0 }, { id: 2, label: "World", color: "green", sorting: 1 }, { id: 3, label: "Hello", color: "blue", sorting: 4 }, { id: 4, label: "Sunshine", color: "yellow", sorting: 5 }, { id: 5, label: "Hello", color: "red", sorting: 6 }],

keys = ['label', 'color'],

filtered = listOfTags.filter(

(s => o =>

(k => !s.has(k) && s.add(k))

(keys.map(k => o[k]).join('|'))

)

(new Set)

);

console.log(filtered);
.as-console-wrapper { max-height: 100% !important; top: 0; }

how to remove first element of duplicate in an array of objects

This will give you an array with the last value for duplicated elements, in preserved order. If you want exactly the 2nd one you need an extra flag

array=[{id:34,value:45}, {id:23,value:35}, {id:34,value:28}]

const obj = array.reduce ( (acc,cur,index) => {
acc[cur.id] = {index:cur};
return acc;
},{});

const output = Object.values(obj).sort( (a,b) => a.index - b.index).map( ({index:val}) => val )

console.log(output)

Remove duplicate objects from an Array

This one will assign this.subMenuItems an array containing only the first instance of each item because indexOf returns the first index where the object is found.

this.subMenuItems = this.items.filter((item, index, self) => self.indexOf(item) === index);

Remove duplicate from array of objects based on value of properties in JavaScript

You can use Map to club values by name and in case there are two values with same name just use the one without type = "new"

let someArray = [{id: 3, name:"apple", type: "new"}, {id: 1, name:"apple"}, {id: 2, name:"mango"}, {id: 4, name:"orange"}, {id: 5, name:"orange", type: "new"}, {id: 6, name: "pineapple", type: "new"}]

function getUnique(arr){
let mapObj = new Map()

arr.forEach(v => {
let prevValue = mapObj.get(v.name)
if(!prevValue || prevValue.type === "new"){
mapObj.set(v.name, v)
}
})
return [...mapObj.values()]
}

console.log(getUnique(someArray))

Remove duplicate objects from an array with Keys

Apply the technique shown in this answer, which is:

function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}

...but using findIndex with some criteria rather than just indexOf.

let people = [
{ name: "George", lastname: "GeorgeLast", age: 12 },
{ name: "George", lastname: "GeorgeLast", age: 13 },
{ name: "Bob", lastname: "GeorgeLast", age: 12 }
]

let result = people.filter(
(person, index) => index === people.findIndex(
other => person.name === other.name
&& person.lastname === other.lastname
));
console.log(result);


Related Topics



Leave a reply



Submit