简体   繁体   English

如何根据 Javascript 中的另一个 object 过滤对象数组

[英]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.谁能帮我知道如何根据另一个 object 条件过滤一组对象。

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:过滤器 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.如果它与按位运算符的执行顺序中的条件匹配,则该数组应返回 object。 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 .您可以使用conditions上的every调用来过滤每个项目。 Just switch on the operator and use item[id] to compare against the value .只需switch operator并使用item[id]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 .或者,您可以使用 object(地图)查找而不是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:您必须从字符串到运算符创建一个 map,然后遍历您的数组:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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