javascript 如何过滤带有可变参数的javascript对象数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17099029/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 07:05:12  来源:igfitidea点击:

How to filter a javascript object array with variable parameters

javascriptfilter

提问by potterzot

I want to select the objects based on the properties of the objects, but not always the same properties. In other words:

我想根据对象的属性选择对象,但并不总是相同的属性。换句话说:

arr = [
    { name: "joe",   age21: 1 },
    { name: "nick",  age21: 0 },
    { name: "blast", age21: 1 }
];

arr.filter(function(item) {
    return (item.name === "nick" && item.age21 === 1);
});

But sometimes I just want to filter on name for example:

但有时我只想过滤名称,例如:

arr.filter(function(item) {
    return (item.name === "nick");
});

What I want to do is generalize this so that the list of parameters can be passed to the function. I've come up with the following, but it's slow and I'm wondering if there is a better way:

我想要做的是概括这一点,以便可以将参数列表传递给函数。我想出了以下方法,但速度很慢,我想知道是否有更好的方法:

filterParams = function(arr, params) {
    var new_array = arr.filter(function(item) {
        var select = 1
        for(obj in params) { //create the filter criteria based on varying set of parameters
            var select = select && params[obj] === item[obj];
        }
        return select;
    });
    return new_array;
}

Then you could call it with: filterParams(arr, {name: "nick", age21: 1});

然后你可以调用它: filterParams(arr, {name: "nick", age21: 1});

or with: filterParams(arr, {name: "nick"});

或与: filterParams(arr, {name: "nick"});

and it would work either way.

无论哪种方式它都可以工作。

In case you're wondering, I'm doing this because I have different data sets that I want to run through the same routine, so the filter properties need to be generalized so that I can filter on properties specific to each dataset.

如果您想知道,我这样做是因为我有不同的数据集要通过相同的例程运行,因此需要对过滤器属性进行概括,以便我可以过滤特定于每个数据集的属性。

Thanks!

谢谢!

回答by elclanrs

Here's a functional approach that should work for any numbers of properties given the object:

这是一个函数式方法,应该适用于给定对象的任意数量的属性:

function filter(arr, criteria) {
  return arr.filter(function(obj) {
    return Object.keys(criteria).every(function(c) {
      return obj[c] == criteria[c];
    });
  });
}

For example:

例如:

var arr = [
  { name: 'Steve', age: 18, color: 'red' },
  { name: 'Louis', age: 21, color: 'blue' }, //*
  { name: 'Mike', age: 20, color: 'green' },
  { name: 'Greg', age: 21, color: 'blue' }, //*
  { name: 'Josh', age: 18, color: 'red' }
];

console.log(filter(arr, { age: 21, color: 'blue' }));
//^ {age:21, color:'blue', name:'Louis}
//  {age:21, color:'blue', name:'Greg'}

Not sure about your performance issue, but this should be OK.

不确定您的性能问题,但这应该没问题。

Edit:You could make this more powerful with regular expressions, something like this:

编辑:您可以使用正则表达式使其更强大,如下所示:

function filter(arr, criteria) {
  return arr.filter(function(obj) {
    return Object.keys(criteria).every(function(c) {
      return new RegExp(criteria[c]).test(obj[c]);
    });
  });
}

console.log(filter(arr, { age: /^2\d$/, color: /^(red|blue)$/ }));
//^ Louis, Greg                --^-- twenty-something
//                                               ----^---- red OR blue