admin管理员组文章数量:1220776
I have an array of objects which I would like to filter using a contains/any method on a property using underscore.
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.
I have an array of objects which I would like to filter using a contains/any method on a property using underscore.
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.
Share Improve this question asked Mar 30, 2016 at 14:42 GrantGrant 931 gold badge1 silver badge8 bronze badges4 Answers
Reset to default 12The JS equivalent of contains
is usually indexOf
(and find
in rare cases).
You can use the built-in Array.prototype.filter
to do this like:
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
:
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.
var allowedPeople = _.filter(people, (p) =>
_.some(allowedAges, (a) => (a.age || a) === p.age)
);
Why not use 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:
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>")
本文标签:
版权声明:本文标题:javascript - Using Underscore.js to filter an array of objects on a property using a 'contains' - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739264881a2155543.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论