Given I have this array of objects:
let array1 = [
{id: 1, name: "Test Item 1", price: 100, type: 'item'},
{id: 1, name: "Test Fee 1", price: 200, type: 'fee'},
{id: 3, name: "Test 3", price: 300, type: 'item'},
]
Now i want to filter so I only have the objects where every key value of the following array match:
let array2 = [
{id: 1, type: "item"},
{id: 3, type: "item"},
]
This should return array1[0]
and array1[2]
.
My current solution has been this, but it wont work if array2 has multiple object keys to compare to:
array1.filter(row => {
return array2.find(r => {
return Object.entries(row).some(([key, value]) => r[key] === value);
})
});