-4

This is the filter:

var filter = {
  address: ['England'],
  name: ['Mark', 'Tom'] // Mark or Tom in this case
};
var users = [{
    name: 'John',
    email: 'johnson@mail.com',
    age: 25,
    address: 'USA'
  },
  {
    name: 'Tom',
    email: 'tom@mail.com',
    age: 35,
    address: 'England'
  },
  {
    name: 'Mark',
    email: 'mark@mail.com',
    age: 28,
    address: 'England'
  }
];

The filtered result should be

  {
    name: 'Tom',
    email: 'tom@mail.com',
    age: 35,
    address: 'England'
  },
  {
    name: 'Mark',
    email: 'mark@mail.com',
    age: 28,
    address: 'England'
  }
CCCC
  • 5,665
  • 4
  • 41
  • 88
  • Already asked, and answered: https://stackoverflow.com/questions/31831651/javascript-filter-array-multiple-conditions – Cristik Sep 26 '21 at 07:09

2 Answers2

2

You take your users, filter it, and see if the name is contained in the filters.

const onlyMarkAndTom = users.filter(user => filter.name.includes(user.name))

Include additional conditions to narrow down the result

const onlyMarkAndTom = users.filter(user => filter.name.includes(user.name) && ...)
Federkun
  • 36,084
  • 8
  • 78
  • 90
0
users.filter((user) => {
  if (!filter.address.includes(user.address)) {
    return false;
  }
  if (!filter.name.includes(user.name)) {
    return false;
  }
  return true;
});
Medo Paw
  • 81
  • 5