Filter object array based on another object array in javascript
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 a 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 end result is an array that only has objects that have the same keys as the second array. (no while or while loop)
In this case it will 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 going through all the keys filter
.
+3
source to share
2 answers
You can use Set
for property names and filter the array.
var array = [{ A: 'red', B: 'blue' }, { Q: 'green', R: 'blue' }, { B: 'green', M: 'red' }, { Q: 'white', R: 'blue' }],
filter = [{ A: 'val', B: 'someval' }, { B: 'anothervalue', M: 'value' }],
getKey = o => Object.keys(o).sort().join('|'),
result = array.filter((s => o => s.has(getKey(o)))(new Set(filter.map(getKey))));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
+3
source to share
You can do it with fiter()
, some()
andevery()
var arr1 = [{"A":"red","B":"blue"},{"Q":"green","R":"blue"},{"B":"green","M":"red"},{"Q":"white","R":"blue"}]
var filter = [{"A":"val","B":"someval"},{"B":"anothervalue","M":"value"}]
var result = arr1.filter(function(e) {
return filter.some(function(a) {
var keyE = Object.keys(e);
var keyA = Object.keys(a);
return keyE.length == keyA.length && keyE.every(function(k) {
return keyA.includes(k)
})
})
})
console.log(result)
0
source to share