Removing elements from an array of objects based on duplicate values of multiple keys
You can use pure functions by using Array.some() and Array.reduce() to reduce your input array to array of distinct elements as shown below
var arr = [
{ type_id: "3", full_empty:"true", quantity:1},
{ type_id: "3", full_empty:"true", quantity:1},
{ type_id: "9", full_empty:"true", quantity:4},
{ type_id: "9", full_empty:"false", quantity:4},
{ type_id: "9", full_empty:"true", quantity:4},
{ type_id: "9", full_empty:"true", quantity:4},
{ type_id: "9", full_empty:"true", quantity:4}
];
var a = arr.reduce(function (accumulator, current) {
if (checkIfAlreadyExist(current)) {
return accumulator
} else {
return accumulator.concat([current]);
}
function checkIfAlreadyExist(currentVal) {
return accumulator.some(function(item){
return (item.type_id === currentVal.type_id &&
item.full_empty === currentVal.full_empty);
});
}
}, []);
console.log(a);
Concise ES6 syntax
A more concise reduce
can be written using ES6 arrow functions and spread operator as below:
var arr = [
{ type_id: "3", full_empty:"true", quantity:1},
{ type_id: "3", full_empty:"true", quantity:1},
{ type_id: "9", full_empty:"true", quantity:4},
{ type_id: "9", full_empty:"false", quantity:4},
{ type_id: "9", full_empty:"true", quantity:4},
{ type_id: "9", full_empty:"true", quantity:4},
{ type_id: "9", full_empty:"true", quantity:4}
];
var a = arr.reduce((accumulator, current) => {
if (checkIfAlreadyExist(current)) {
return accumulator;
} else {
return [...accumulator, current];
}
function checkIfAlreadyExist(currentVal) {
return accumulator.some((item) => {
return (item.type_id === currentVal.type_id &&
item.full_empty === currentVal.full_empty);
});
}
}, []);
console.log(a);
Despite the other solutions, I suggest to use a hash table with type_id
and full_empty
as key and if a new found then set hash to true. Together with Array#filter
, you get a new array with the unique items.
var arr = [{ type_id: "3", full_empty: "true", quantity: 1 }, { type_id: "3", full_empty: "true", quantity: 1 }, { type_id: "9", full_empty: "true", quantity: 4 }, { type_id: "9", full_empty: "false", quantity: 4 }, { type_id: "9", full_empty: "true", quantity: 4 }, { type_id: "9", full_empty: "true", quantity: 4 }, { type_id: "9", full_empty: "true", quantity: 4 }],
filtered = arr.filter(function (a) {
var key = a.type_id + '|' + a.full_empty;
if (!this[key]) {
this[key] = true;
return true;
}
}, Object.create(null));
console.log(filtered);
ES6
var arr = [{ type_id: "3", full_empty: "true", quantity: 1 }, { type_id: "3", full_empty: "true", quantity: 1 }, { type_id: "9", full_empty: "true", quantity: 4 }, { type_id: "9", full_empty: "false", quantity: 4 }, { type_id: "9", full_empty: "true", quantity: 4 }, { type_id: "9", full_empty: "true", quantity: 4 }, { type_id: "9", full_empty: "true", quantity: 4 }],
filtered = arr.filter(
(temp => a =>
(k => !temp[k] && (temp[k] = true))(a.type_id + '|' + a.full_empty)
)(Object.create(null))
);
console.log(filtered);