如何用可变参数过滤JavaScript对象数组 [英] How to filter a javascript object array with variable parameters

查看:150
本文介绍了如何用可变参数过滤JavaScript对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  arr = [
{name:joe,age21:1},
{姓名:nick,年龄21:0},
{姓名:爆炸,年龄21:1}
];
$ b $ arr.filter(function(item){
return(item.name ===nick&& item.age21 === 1);
} );

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

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

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

$ p $ filterParams =函数(arr,params){
var new_array = arr.filter(function(item){
var select = 1
for(obj in params){//根据不同的参数集
var select = select&& params [obj] === item [obj];
}
return select;
});
返回new_array;

$ / code>

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




filterParams(arr,{name:nick});



b
$ b

如果你想知道,我这样做是因为我有不同的数据集,我想通过相同的例程运行,所以过滤器的属性需要一般化,以便我可以过滤每个数据集特有的属性。

谢谢! 解决方案

这是一个函数式的方法,它可以用于给定对象的任何数量的属性:
$ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $函数过滤器(arr,criteria){
return arr.filter(function(obj){
return Object.keys(criteria).every(function(c){
return obj [c] == criteria [c];
});
});例如:




$ b pre $ var arr = [
{name:'Steve',age:18,color:'red'},
{name:'Louis',age: 21,color:'blue'},// *
{name:'Mike',年龄:20,颜色:'green'},
{name:'Greg',年龄:21,颜色:'blue'},// *
{name:'Josh',年龄:18,颜色:'red'}
];

console.log(filter(arr,{age:21,color:'blue'}));
// ^ {age:21,color:'blue',name:'Louis}
// {年龄:21,颜色:'blue',名称:'Greg'}

不知道您的效能问题,但应该可以。



  

函数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 - ^ - 二十几岁
// ---- ^ ----红色或蓝色


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});

or with: 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!

解决方案

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

这篇关于如何用可变参数过滤JavaScript对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆