Reputation: 477
I've succeeded to retrieve the array which contains the product (filter by an ID) but I got empties arrays in the output too.
Here the piece of code I wrote for the example:
var category = [{
"reference": 'BC-ENFANT',
"name": 'Pour les Enfants',
"description": 'Soins pour les enfants...',
"id": 155,
"productList": [{
"id": 13655,
"reference": 'PROD_ENFANT_01',
"name": 'Brushing'
},
{
"id": 13656,
"reference": 'PROD_ENFANT_03',
"name": 'Soins'
},
]
},
{
"reference": 'BC-FEMME',
"name": 'Pour les Femmes',
"description": 'Prestations pour les femmes',
"id": 154,
"productList": [{
"id": 13657,
"reference": 'PROD_ENFANT_01',
"name": 'Brushing'
},
{
"id": 13658,
"reference": 'PROD_ENFANT_03',
"name": 'Soins'
},
]
}
];
var productList = category.map(p => {
return p.productList
});
var product = productList.map(p => p.filter(p => p.id === 13657).map(pp => {
return {
Reference: pp.reference,
Name: pp.name,
Quantity: 1
}
}));
console.log(product)
Upvotes: 1
Views: 41
Reputation: 337666
My goal is to retrieve the product object from all the productList
In this case you can use map()
to create a multi-dimensional array of them and then flat()
to combine it in to a single dimension array, like this:
var category = [{"reference":'BC-ENFANT',"name":'Pour les Enfants',"description":'Soins pour les enfants...',"id":155,"productList":[{"id":13655,"reference":'PROD_ENFANT_01',"name":'Brushing'},{"id":13656,"reference":'PROD_ENFANT_03',"name":'Soins'},]},{"reference":'BC-FEMME',"name":'Pour les Femmes',"description":'Prestations pour les femmes',"id":154,"productList":[{"id":13657,"reference":'PROD_ENFANT_01',"name":'Brushing'},{"id":13658,"reference":'PROD_ENFANT_03',"name":'Soins'}]}];
var productList = category.map(p => p.productList).flat();
console.log(productList)
Upvotes: 1