I'm trying to filter an array, like for example the I want it to return all the color, sizes and etc., I know you can it filter it by doing something like this :
const products = [
{
name: "Product A",
attributes: [
{
color: "Yellow",
size: "Small",
price: 100,
quantity: 30,
},
{
color: "Yellow",
size: "Medium",
price: 150,
quantity: 20,
},
{
color: "Yellow",
size: "Large",
price: 200,
quantity: 10,
},
{
color: "Red",
size: "Small",
price: 100,
quantity: 15,
},
{
color: "Red",
size: "Medium",
price: 150,
quantity: 10,
},
{
color: "Red",
size: "Large",
price: 200,
quantity: 5,
},
{
color: "Blue",
size: "Small",
price: 100,
quantity: 15,
},
{
color: "Blue",
size: "Medium",
price: 150,
quantity: 10,
},
{
color: "Blue",
size: "Large",
price: 200,
quantity: 5,
},
],
},
];
const result = data.map(({ attributes }) =>
attributes.filter(({ color }) => color === "Red")
);.
console.log(result)
But what if there are like 100 attribute color and sizes, how to maybe like separate them via there color sample if yellow array it will return all the yellow data like this
[
{
color: "Yellow",
size: "Small",
price: 100,
quantity: 30,
},
{
color: "Yellow",
size: "Medium",
price: 150,
quantity: 20,
},
{
color: "Yellow",
size: "Large",
price: 200,
quantity: 10,
},
]
for sizes it will return for example small it will return all the small data like this:
[{
color: "Yellow",
size: "Small",
price: 100,
quantity: 30,
},
{
color: "Red",
size: "Small",
price: 100,
quantity: 15,
},
{
color: "Blue",
size: "Small",
price: 100,
quantity: 15,
}]
If my question isn't that clear, comment down below so I could explain further. Thanks for your help