const arr = [
{
"id" : 1,
"city" : "Москва",
"title" : "ООО Мавзолей",
"type" : ["носки", "аромат"]
},
{
"id" : 2,
"city" : "Татарск",
"title" : "ООО Татарск Продакс",
"type" : ["коровы", "ЖБ плиты"]
}
];
const filter = {
city: `Татарск`,
title: ``,
type: `коровы`
};
const arr = [
{
id : 1,
city : "Москва",
title : "ООО Мавзолей",
type : ["носки", "аромат"]
},
{
id : 2,
city : "Татарск",
title : "ООО Татарск Продакс",
type : ["коровы", "ЖБ плиты"]
}
];
const filter = {
city: `Татарск`,
title: ``,
type: `коровы`
};
const data = arr.filter(item => {
const title = filter.title === "" ? item.title : filter.title;
return (item.city === filter.city && item.title === title && item.type.includes(filter.type)) ? item : null;
});
console.log(data);
const comparators = [
[ 'city', (itemVal, filterVal) => itemVal === filterVal ],
[ 'title', (itemVal, filterVal) => itemVal.includes(filterVal) ],
[ 'type', (itemVal, filterVal) => itemVal.includes(filterVal) ],
];
const filteredArr = arr.filter(n => comparators.every(([ k, f ]) => f(n[k], filter[k])));