I have an Array of Objects.
Each array of Objects contains an array items and each item in this array is an Object containing an array category:
var obj = [
// first object
{
label: 'Label 1',
// first items
items: [
{
id: 1,
itemName: 'Item Name 1',
img: 'imgs/path-to1.jpeg',
sizes: [],
colors: [],
// first category
category: [
'I',
'E',
'M'
],
defaultChoices: {}
},
{
id: 2,
itemName: 'Item Name 2',
img: 'imgs/path-to2.jpeg',
sizes: [],
colors: [],
// second category
category: [
'I',
'E'
],
defaultChoices: {}
},
{
id: 3,
itemName: 'Item Name 3',
img: 'imgs/path-to3.jpeg',
sizes: [],
colors: [],
// third category
category: [
'I'
],
defaultChoices: {}
},
]
},
// second object
{
label: 'Label 2',
// second items
items: [
{
id: 7,
itemName: 'Item Name 7',
img: 'imgs/path-to7.jpeg',
sizes: [],
colors: [],
// fourth category
category: [
'I',
'M'
],
defaultChoices: {}
},
...
just to make the things clearer, a typical direct access to category would be performed this way: obj[0].items[0].category.
From the frontend of the app, the user, based on her choices, can send to the application one of the following arrays:
['I'];['E'];['M'];['I','E'];['I','M'];['E','M'];['I','E','M'];- ...;
Then, the application should return the obj array filtered: if, for example, the user sent ['I'], the array should contain any objects where category contains 'I'. If the user sent ['E','M'], the array should contain any object where category contains ['E','M'] (no matter if category is ['E','M','I']), and so on.
I red a lot of docs about the JS filter function but I was not able to develop one function that, given the user array, can return a new array as result of filtering obj. I found dozen of docs with non-real-life examples like this one:
var hs = [
{name: 'Batman', franchise: 'DC'},
{name: 'Ironman', franchise: 'Marvel'}
];
var marvels = heroes.filter(function(h) {
return hs.franchise == 'Marvel';
});
Any help is appreciated.
[UPDATE] I add a more realistic data sample, sorry for not providing it before: https://drive.google.com/open?id=1sdRx6sQ-cnRXJ8YCe4QH2Sy5fa5mopYW