const products = [
{
"id": 1,
"category": "footwear",
"gender": "man",
"brand": "Topper",
"model": "classic",
"color": "black",
"price": 1800,
"sizeStock": [{"size": 34, "stock": 0}, {"size": 35, "stock": 3}, {"size": 36, "stock": 5}]
},
{
"id": 2,
"category": "footwear",
"gender": "man",
"brand": "Topper",
"model": "rainbow",
"color": "multi",
"price": 3500,
"sizeStock": [{"size": 139, "stock": 1},{"size": 40, "stock": 5},{"size":41, "stock":1}]
}]
I want to filter products using the size value that is inside of sizeStock array that is inside of products array.
products.forEach((product) => {
product.sizeStock.filter((a) => a.size === 40);
});
For example I try this to filter all the products that have the size 40. But it´s not working.
You can easily achieve this result using filter and some as
products.filter((p) => p.sizeStock.some((o) => o.size === 40))
const products = [
{
id: 1,
category: "footwear",
gender: "man",
brand: "Topper",
model: "classic",
color: "black",
price: 1800,
sizeStock: [
{ size: 34, stock: 0 },
{ size: 35, stock: 3 },
{ size: 36, stock: 5 },
],
},
{
id: 2,
category: "footwear",
gender: "man",
brand: "Topper",
model: "rainbow",
color: "multi",
price: 3500,
sizeStock: [
{ size: 139, stock: 1 },
{ size: 40, stock: 5 },
{ size: 41, stock: 1 },
],
},
];
const result = products.filter((p) => p.sizeStock.some((o) => o.size === 40));
console.log(result);