我想使用属性值数组筛选集合。给定一个ID数组,返回具有匹配ID的对象。有没有使用lodash
/underscore
的快捷方法?lodash使用数组值的筛选器集合
var collections = [{ id: 1, name: 'xyz' },
{ id: 2, name: 'ds' },
{ id: 3, name: 'rtrt' },
{ id: 4, name: 'nhf' },
{ id: 5, name: 'qwe' }];
var ids = [1,3,4];
// This works, but any better way?
var filtered = _.select(collections, function(c){
return ids.indexOf(c.id) != -1
});
不是。但是你可以直接处理Arrays原型的'filter()'方法;看起来更干净:) – gustavohenke