简体   繁体   English

使用Underscore.js使用'contains'过滤属性上的对象数组

[英]Using Underscore.js to filter an array of objects on a property using a 'contains'

I have an array of objects which I would like to filter using a contains/any method on a property using underscore. 我有一个对象数组,我想使用下划线在属性上使用contains / any方法进行过滤。

For example, if I had the following variables: 例如,如果我有以下变量:

var people = [
{
    name: 'Dave',
    age: 26
},
{
    name: 'Frank',
    age: 23
}];
var allowedAges = [20, 23, 24];

I would like to use underscore to end up with a result like: 我想用下划线结果如下:

 var allowedPeople = [];
 _.each(_.where(people, { age: _.any()}), function (person) {
            allowedPeople.push(person);
        });

And there may also be occasions where allowedAges is an array of objects and id want to use the contains/any on the people array using a property of the objects in allowedAges. 并且有些情况下allowedAges是一个对象数组,而id想要使用allowedAges中对象的属性在people数组上使用contains / any。

The JS equivalent of contains is usually indexOf (and find in rare cases). JS的等价物contains通常是indexOf (在极少数情况下find )。

You can use the built-in Array.prototype.filter to do this like: 您可以使用内置的Array.prototype.filter来执行以下操作:

people.filter(function (person) {
  return allowedAges.indexOf(person.age) !== -1; // -1 means not present
});

or with underscore, using the same predicate: 或者使用下划线,使用相同的谓词:

_.filter(people, function (person) {
  return allowedAges.indexOf(person.age) !== -1; // -1 means not present
}

If you have access to ES6 collections or a polyfill for Set , you can replace the allowedAges array with a set (enforcing unique values) and simply use Set.prototype.has : 如果您可以访问ES6集合或SetallowedAges ,则可以使用set(强制唯一值)替换allowedAges数组,并只使用Set.prototype.has

people.filter(person => allowedAges.has(person.age))

For the sake of completeness, here's an underscore solution that works with a mixed array of integers and objects containing an age property. 为了完整起见,这里是一个下划线解决方案,它使用包含age属性的整数和对象的混合数组。

var allowedPeople = _.filter(people, (p) =>
  _.some(allowedAges, (a) => (a.age || a) === p.age)
);

Why not use vanilla js filter ? 为什么不使用vanilla js filter

var people = [
  {
    name: 'Dave',
    age: 26
  },
  {
    name: 'Frank',
    age: 23
  }
];

var allowedAges = [20, 23, 24];

var allowedPeople = people.filter(function((person, i, arr) {
  return allowedAges.indexOf(person.age) !== -1;
});

This is a pure JS version: 这是一个纯JS版本:

 var people = [{ name: 'Dave', age: 26 }, { name: 'Frank', age: 23 }]; var allowedAges = [20, 23, 24]; function filterData(arr, key, searchList){ var result = []; if(Array.isArray(searchList)){ result = arr.filter(function(item){ return (searchList.indexOf(item[key])>-1); }); } else{ result = arr.filter(function(item){ return (searchList === item[key]); }); } return result; } document.write("<pre>" + JSON.stringify(filterData(people,"age", allowedAges), 0, 4) + "</pre>") 

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

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