The code below works smoothly for the searches I made:
const selectedFilters = {
color: ["Red", "Blue"],
type: ["Shirt"],
size: ["M"]
};
const items = [
{
name: "Item 1",
filters: {
color: ["Red", "Blue", "Black"],
type: ["Shirt"],
size: ["M"]
}
},
{
name: "Item 2",
filters: {
color: ["Red"],
type: ["Pants"],
size: ["M"]
}
}
];
const filterArr = Object.values(selectedFilters).flat();
const output = items.filter(({filters}) => {
const objFilters = Object.values(filters).flat();
return filterArr.every(val => objFilters.includes(val));
})
console.log(output);
On the other hand, when I search for 2 values I want it to work as "OR" not "AND"
e.g. when I search as
const selectedFilters = {
color: ["Red"],
type: ["Shirt", "Pants"],
size: ["M"]
};
how can I make the engine to bring results containing shirts or pants, so both Item 1 and 2?
P.S. Currently an empty filter brings all the results, e.g.
const selectedFilters = {
color: [],
type: [],
size: []
};
which is something I want to keep as a feature.