My Products page consist of Product Items from a fetch API call from a local URL (I have included the array returned from the API call here which is stored in products variable). It has 6 Items with 3 being shoes and 3 being headphones, Shoes item has a nested property object called skus while headphones don't have it.
I want my product page to show all 6 items but when I select the size selector dropdown in my page, I want to filter only shoes with that specific size (size is a object property of skus).
Error says it is with the ternary operator, can someone clarify me, what's wrong in ternary syntax.
Thanks.
Array returned from API call
{
"id": 1,
"category": "shoes",
"image": "shoe1.jpg",
"name": "Hiker",
"price": 94.95,
"skus": [
{ "sku": "17", "size": 7 },
{ "sku": "18", "size": 8 }
],
"description": "This rugged boot will get you up the mountain safely."
},
{
"id": 2,
"category": "shoes",
"image": "shoe2.jpg",
"name": "Climber",
"price": 78.99,
"skus": [
{ "sku": "28", "size": 8 },
{ "sku": "29", "size": 9 }
],
"description": "Sure-footed traction in slippery conditions."
},
{
"id": 3,
"category": "shoes",
"image": "shoe3.jpg",
"name": "Explorer",
"price": 145.95,
"skus": [
{ "sku": "37", "size": 7 },
{ "sku": "38", "size": 8 },
{ "sku": "39", "size": 9 }
],
"description": "Look stylish while stomping in the mud."
},
{
"id": 4,
"category": "headphone",
"image": "headphone3.jpg",
"name": "headphone red",
"price": 100,
"description": "Red colored headphone"
},
{
"id": 5,
"category": "headphone",
"image": "headphone2.jpg",
"name": "headphone blue",
"price": 90,
"description": "Blue colored headphone"
},
{
"id": 6,
"category": "headphone",
"image": "headphone1.jpg",
"name": "headphone black",
"price": 80,
"description": "Black colored headphone"
}
Product filtering code when I select size in dropdown
const filteredProducts = size
? products.filter((element) => {
element.skus
? products.filter((element) => {
element.skus.find(
(element_skus) => element_skus.size === parseInt(size)
);
})
: products;
})
: products;
Error referes to lines
element.skus
in the product filtering code
Error image error