javascript filter array of objects by multiple properties code example
Example 1: filter using two array of objects
const arr1 = [{id:'1',name:'A'},{id:'2',name:'B'},{id:'3',name:'C'},{id:'4',name:'D'}];
const arr2 = [{id:'1',name:'A',state:'healthy'},{id:'3',name:'C',state:'healthy'}];
const filterByReference = (arr1, arr2) => {
let res = [];
res = arr1.filter(el => {
return !arr2.find(element => {
return element.id === el.id;
});
});
return res;
}
console.log(filterByReference(arr1, arr2));
Example 2: filter js object array based on multiple parameters
var arr= [{id: "123", name: "Foo"},
{id: "123", name: "Bar"},
{id: "345", name: "Foo"},
{id: "678", name: "FooBar"}
];
var name = 'Foo';
var id = '123';
arr = arr.filter(function(elem) {
return !(elem.id == id && elem.name == name)
});
Example 3: javascript find object in array by multiple property values
let serverProposal = lobby.proposals.find( (p) => p.author == userID && p.isSubmitted == false);