Answer the question
In order to leave comments, you need to log in
How to filter an array with objects?
It is necessary to filter the array by object:
const arr = [
{
"id" : 1,
"city" : "Москва",
"title" : "ООО Мавзолей",
"type" : ["носки", "аромат"]
},
{
"id" : 2,
"city" : "Татарск",
"title" : "ООО Татарск Продакс",
"type" : ["коровы", "ЖБ плиты"]
}
];
const filter = {
city: `Татарск`,
title: ``,
type: `коровы`
};
Answer the question
In order to leave comments, you need to log in
You at least correct the errors in your code once you ask for help.
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);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question