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
Add a Property to a JavaScript Object Using a Variable as the Name
How to Manage a Redirect Request After a Jquery Ajax Call
Why Can't I Directly Modify a Component'S State, Really
How to Completely Uninstall Node.Js, and Reinstall from Beginning (Mac Os X)
Chrome Extension Message Passing: Response Not Sent
Disable/Enable an Input With Jquery
How to Export JavaScript Array Info to CSV (On Client Side)
Why Doesn't My Arrow Function Return a Value
How to Set Object Property (Of Object Property Of..) Given Its String Name in JavaScript
How to Sort an Object Array by Date Property
What Does the Comma Operator Do in JavaScript
_Proto_ Vs. Prototype in JavaScript
Negative Lookbehind Equivalent in JavaScript
Rails 4: How to Use $(Document).Ready() With Turbo-Links