I have an array of objects in this format:
var full_list = [
{
"pid": 1,
"items":[
{"item_id": '9'},
{"item_id": '10'},
{"item_id": '12'}
]
},
{
"pid": 2,
"items":[
{"item_id": '33'},
{"item_id": '22'},
{"item_id": '65'}
]
}...
];
I have a tmp array that consists of objects from a full array:
var tmp_list = [
{
"pid": 2,
"items":[
{"item_id": '33'},
{"item_id": '22'},
{"item_id": '65'}
]
}, {....}
I would like to filter objects from the full list, where in the id array of the object's object at least one of the selectedID values appears.
var selectedIDs = {'1', '9', '45', ....};
and then add them to the tmp list.
I tried to use filters, but could not fully understand this.
Thank.
selectedIDs.forEach(function(id) {
var tmp = full_list.filter(function (obj) {
obj.items.forEach(function (item) {
if (item.id === id) {
console.log('found');
}
});
});
tmp_list.push(tmp);
});
source
share