I am trying to create a filter that is working when the object is a single value, but when introducing an array of keywords I am having issues.
My code looks like the following:
const filter = {
colors: ["white"],
sizes: [9, 12],
brands: ["adidas"],
keywords: ["running", "weights"]
};
const shoes = [{
brand: "adidas",
size: 9,
color: "white",
keywords: ["running"]
},
{
brand: "adidas",
size: 12,
color: "white",
keywords: ["weigths"]
},
{
brand: "nike",
size: 7,
color: "red",
keywords: ["running", "tennis"]
}
];
const properties = {
colors: 'color',
sizes: 'size',
brands: 'brand',
keywords: 'keywords',
}
const filters = Object
.entries(filter)
.filter(([, {
length
}]) => length)
.map(([k, v]) => [properties[k], v]);
const result = shoes.filter(shoe => filters.every(([k, v]) => v.includes(shoe[k])));
console.log('result', result)
The result that I am looking for is
const results = {
brand: "nike",
size: 9,
color: "white",
keywords: ["running"]
},
{
brand: "adidas",
size: 12,
color: "white",
keywords: ["swimming"]
}]
sizeandcolor?