R
R
Roman Andreevich2020-04-20 18:21:06
JavaScript
Roman Andreevich, 2020-04-20 18:21:06

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

1 answer(s)
L
Loli E1ON, 2020-04-20
@RomanDillerNsk

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 question

Ask a Question

731 491 924 answers to any question