Let's say I have a data
and a map
.
let data = [
{
'name': 'bob',
'items': ['111']
},
{
'name': 'Jane',
'items': ['111']
},
{
'name': 'Greg',
'items': ['222']
}
]
let item_map = [
{ 'Item1': '111', 'Item2': '222'}
]
The items
in data
object contains the ids
of the item
. What I am trying to do is to filter out the object from data
where using a list of values matched to the id
of the map.
For example, given ['Item1'], I want to get
[{
'name': 'bob',
'items': ['111']
},
{
'name': 'Jane',
'items': ['111']
}]
What I tried is
data.filter( item => ['111'].some(filter => (item_map["Item1"]).includes(filter)))
But this keeps giving me a Uncaught TypeError: Cannot read property 'includes' of undefined
error.
EDIT
data.filter(item => ['111'].some(filter => item_map[0]["Item1"].includes(filter)))
This is what I newly tried, but this just returns all three items.
I can't exactly understand why you'd choose an array for an items "map", but to conform with your current data structure, this is the correct logic:
let filters = ['Item1'];
data.filter((datum) => {
return filters.some((filter) => datum.items.includes(item_map[0][filter]));
});
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments