I am attempting to filter an array based on specific attributes, such as color and size. I understand that it can be filtered using code like this:
const products = [
{
name: "Product A",
attributes: [
{
color: "Yellow",
size: "Small",
price: 100,
quantity: 30,
},
...
],
},
];
const result = data.map(({ attributes }) =>
attributes.filter(({ color }) => color === "Red")
);
console.log(result)
However, what if there are numerous attribute colors and sizes? Is there a way to separate them by their color? For example, filtering all yellow data:
[
{
color: "Yellow",
size: "Small",
price: 100,
quantity: 30,
},
...
]
Similarly, for sizes, how would I filter and return all small-sized data like this:
[{
color: "Yellow",
size: "Small",
price: 100,
quantity: 30,
},
...
]
If my query is unclear, please leave a comment below so I can provide further clarification. Thank you for your assistance.