Source: Removing elements from an array of objects based on duplicate values of multiple keys
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
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); |