Этот фильтр фактически открыт.
Но я думаю, что вы ищете что-то, где некоторые элементы И и другие ИЛИ
Итакесли пользователь выбирает размер , вы хотите отфильтровать их.
Но color - это вариант ||
?
Если это так, я исправлю эту модель.Дайте мне знать.
const p = {
"products": [
{
"tags": ["filter-color-White", "filter-style-Low 1", "5", "6", "7", "8", "9", "10", "11", "12", "13"],
"styles": ["filter-style-Low 1"],
"colors": ["filter-color-White"],
"sizes": ["5", "6", "7", "8", "9", "10", "11", "12", "13"]
},
{
"tags": ["filter-color-Black", "filter-color-Red", "filter-color-Blue", "filter-style-Boot", "7", "8", "9", "12", "13"],
"styles": ["filter-style-Boot"],
"colors": ["filter-color-Black", "filter-color-Red", "filter-color-Blue"],
"sizes": ["7", "8", "9", "12", "13"]
},
{
"tags": ["filter-color-Black", "filter-color-Red", "filter-style-Gat", "7", "8", "9", "10", "11", "12", "13"],
"styles": ["filter-style-Gat"],
"colors": ["filter-color-Black", "filter-color-Red"],
"sizes": ["7", "8", "9", "10", "11", "12", "13"]
}
]
};
const getFiltered = ( ({products}, filterValues) =>
products.filter(p => Object.entries(p)
.flatMap(([k, v]) => v)
.some(entry => filterValues.includes(entry))));
console.log(getFiltered(p, ["5", "filter-style-Gat"]));
Предполагая, что нам нужны некоторые условия:
const p = {
"products": [{
"tags": ["filter-color-White", "filter-style-Low 1", "5", "6", "7", "8", "9", "10", "11", "12", "13"],
"styles": ["filter-style-Low 1"],
"colors": ["filter-color-White"],
"sizes": ["5", "6", "7", "8", "9", "10", "11", "12", "13"]
},
{
"tags": ["filter-color-Black", "filter-color-Red", "filter-color-Blue", "filter-style-Boot", "7", "8", "9", "12", "13"],
"styles": ["filter-style-Boot"],
"colors": ["filter-color-Black", "filter-color-Red", "filter-color-Blue"],
"sizes": ["7", "8", "9", "12", "13"]
},
{
"tags": ["filter-color-Black", "filter-color-Red", "filter-style-Gat", "7", "8", "9", "10", "11", "12", "13"],
"styles": ["filter-style-Gat"],
"colors": ["filter-color-Black", "filter-color-Red"],
"sizes": ["7", "8", "9", "10", "11", "12", "13"]
}
]
};
const getFiltered = (({ products }, {
tags,
styles,
colors,
sizes
}) => {
// Filter size fully.
return products.filter(({
sizes: s
}) => (sizes) ? s.some(size => sizes.includes(size)) : true)
// Now color
.filter(({
colors: c
}) => (colors) ? c.some(color => colors.includes(color)) : true)
// style etc.
.filter(({
styles: s
}) => (styles) ? s.some(style => styles.includes(style)) : true)
});
const filter = {
sizes: ["6", "7"],
colors: ["filter-color-Red"]
};
console.log(getFiltered(p, filter));