1

I have below type of filters array, each key has a single array with its filter. Now I want to filter my main object according to the below filter value. I found one solution for that, but it's for a single value, I want to make it multiple values as per below. Here is the link to the solution which I found.

let filter = [];
filter['address'] = ["England", "UK"];
filter['name'] = ["Mark", "TOM"];

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'
}];

javascript filter array multiple conditions

Amv
  • 59
  • 8
  • 2
    what you tried so far? – Nur Jul 12 '21 at 12:03
  • 1
    you'll need to adapt the code in the solution you've found to use array includes method to see if the filter contains a match – Bravo Jul 12 '21 at 12:04
  • 1
    Use `Object.entries` to get an array of key-values to filter on i.e. `Object.entries(filter).forEach(([key, values]) => users = users.filter(u => values.includes(u[key])));` – nbokmans Jul 12 '21 at 12:04

0 Answers0