Filter array of objects based on another array of objects in javascript

Viewed 720

I have an array like this

arr1 =
[
   {A: 'red', B: 'blue'},
   {Q: 'green', R: 'blue'},
   {B: 'green', M: 'red'},
   {Q: 'white', R: 'blue'},
   ...
]

Each object has two key/value pairs, a letter and color. I have another array like this

filter=
[
   {A: 'val', B: 'someval'},
   {B: 'anothervalue', M: 'value'}
]

Is it possible to filter the first array such that the final result is an array that only has the objects that have the same keys as the second array. (without a for or while loop)

In this scenario it would be:

[
   {A: 'red', B: 'blue'},
   {B: 'green', M: 'red'}
]

So I want something like:

let filteredArr = arr1.filter(obj => 
    Object.keys(obj) == Object.keys(filter[someKey]));

But I'm not sure how to do this without looping over all the keys of filter.

2 Answers
Related