简体   繁体   中英

How to filter array of objects based on another object in Javascript

Can anyone help me know how to filter an array of objects based on another object with conditions.

Sample array

const arrayToFilter=  [   {
        name: 'Arlin Schistl',
        screen_name: 'aschistl1c',
        followers_count: 101,
        following_count: 657,
        location: 'Indonesia',
        verified: true,  
        },  
        {
        name: 'Henka Perren',
        screen_name: 'hperren1d',
        followers_count: 170,
        following_count: 422,
        location: 'Mexico',
        verified: true,   }, ]

Filter Object:

const conditions=[ 
    { 
        id: 'name', 
        operator: 'CONTAINS' 
        value: 'Bob', 
    },
    { 
        condition:'OR',
        id: 'followers_count', 
        operator: 'GTE' 
        value: 200, 
    }, 
    {
        condition:'AND',
        id: 'following_count', 
        operator: 'LTE' 
        value: 10,
    },
    {
        condition:'AND',
        id: 'followers_count', 
        operator: 'GTE' 
        value: 150,
    } 
  ]

The array should return the object if it matches conditions in bitwise operator's execution order. Please let me know what will be the optimized code for this. Thanks in advance!

You could filter each item using an every call on the conditions . Just switch on the operator and use item[id] to compare against the value .

I changed the second item to the following so that it adhears to the constraints:

{
  name: 'Bob Perren',
  screen_name: 'hperren1d',
  followers_count: 300,
  following_count: 5,
  location: 'Mexico',
  verified: true,
}

 const filterWithConditions = (arr, conditions) => arr.filter(item => conditions.every(({ id, operator, value }) => { switch (operator) { case 'CONTAINS': return item[id].indexOf(value) > -1; case 'GTE': return item[id] >= value; case 'LTE': return item[id] <= value; default: return false; } })); const arrayToFilter = [{ name: 'Arlin Schistl', screen_name: 'aschistl1c', followers_count: 101, following_count: 657, location: 'Indonesia', verified: true, }, { name: 'Bob Perren', screen_name: 'hperren1d', followers_count: 300, following_count: 5, location: 'Mexico', verified: true, }] const conditions = [ { id: 'name', operator: 'CONTAINS', value: 'Bob' }, { id: 'followers_count', operator: 'GTE', value: 200 }, { id: 'following_count', operator: 'LTE', value: 10 } ]; const filtered = filterWithConditions(arrayToFilter, conditions); console.log(filtered);
 .as-console-wrapper { top: 0; max-height: 100%;important; }

Alternatively, you could use a object (map) lookup instead of a switch .

const operators = {
  CONTAINS : (a, b) => a.indexOf(b) > -1,
  GTE      : (a, b) => a >= b,
  LTE      : (a, b) => a <= b
}

const filterWithConditions = (arr, conditions) =>
  arr.filter(item => conditions.every(({ id, operator, value }) =>
    operators[operator](item[id], value)));

You have to create a map from your string to the operator, and then just loop over your array:

const operators = {
   "CONTAINS" : (...) => {...}
   "GTE" : (...) => {...}
   "LTE" : (...) => {...}
}
let res = arrayToFilter.filter( el => {
   return conditions.every( cond => operators(el[id]))
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM