lodash使用数组的过滤器集合(lodash Filter collection using array of values)

我想使用属性值数组过滤一个集合。 给定一组ID,返回具有匹配ID的对象。 有没有使用lodash / underscore快捷方法?

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

I would like to filter a collection using array of property value. Given an array of IDs, return objects with matching IDs. Is there any shortcut method using lodash/underscore?

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

                

最满意答案

如果你打算使用这种模式,你可以像下面那样创建一个mixin,但是它并没有与原始代码有任何不同。 它只是使它更加开发人员友好。

_.mixin({ 'findByValues': function(collection, property, values) { return _.filter(collection, function(item) { return _.contains(values, item[property]); }); } });

那么你可以这样使用它。

var collections = [ {id: 1, name: 'xyz'}, {id: 2, name: 'ds'}, {id: 3, name: 'rtrt'}, {id: 4, name: 'nhf'}, {id: 5, name: 'qwe'} ]; var filtered = _.findByValues(collections, "id", [1,3,4]);

更新 - 以上答案是老而笨重的。 请使用Adam Boduch的答案提供更优雅的解决方案。

_(collections) .keyBy('id') // or .indexBy() if using lodash 3.x .at(ids) .value();

If you're going to use this sort of pattern a lot, you could create a mixin like the following, though, it isn't doing anything fundementally different than your original code. It just makes it more developer friendly.

_.mixin({ 'findByValues': function(collection, property, values) { return _.filter(collection, function(item) { return _.contains(values, item[property]); }); } });

Then you can use it like this.

var collections = [ {id: 1, name: 'xyz'}, {id: 2, name: 'ds'}, {id: 3, name: 'rtrt'}, {id: 4, name: 'nhf'}, {id: 5, name: 'qwe'} ]; var filtered = _.findByValues(collections, "id", [1,3,4]);

Update - This above answer is old and clunky. Please use the answer from Adam Boduch for a much more elegant solution.

_(collections) .keyBy('id') // or .indexBy() if using lodash 3.x .at(ids) .value();lodash使用数组的过滤器集合(lodash Filter collection using array of values)

我想使用属性值数组过滤一个集合。 给定一组ID,返回具有匹配ID的对象。 有没有使用lodash / underscore快捷方法?

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

I would like to filter a collection using array of property value. Given an array of IDs, return objects with matching IDs. Is there any shortcut method using lodash/underscore?

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

                

最满意答案

如果你打算使用这种模式,你可以像下面那样创建一个mixin,但是它并没有与原始代码有任何不同。 它只是使它更加开发人员友好。

_.mixin({ 'findByValues': function(collection, property, values) { return _.filter(collection, function(item) { return _.contains(values, item[property]); }); } });

那么你可以这样使用它。

var collections = [ {id: 1, name: 'xyz'}, {id: 2, name: 'ds'}, {id: 3, name: 'rtrt'}, {id: 4, name: 'nhf'}, {id: 5, name: 'qwe'} ]; var filtered = _.findByValues(collections, "id", [1,3,4]);

更新 - 以上答案是老而笨重的。 请使用Adam Boduch的答案提供更优雅的解决方案。

_(collections) .keyBy('id') // or .indexBy() if using lodash 3.x .at(ids) .value();

If you're going to use this sort of pattern a lot, you could create a mixin like the following, though, it isn't doing anything fundementally different than your original code. It just makes it more developer friendly.

_.mixin({ 'findByValues': function(collection, property, values) { return _.filter(collection, function(item) { return _.contains(values, item[property]); }); } });

Then you can use it like this.

var collections = [ {id: 1, name: 'xyz'}, {id: 2, name: 'ds'}, {id: 3, name: 'rtrt'}, {id: 4, name: 'nhf'}, {id: 5, name: 'qwe'} ]; var filtered = _.findByValues(collections, "id", [1,3,4]);

Update - This above answer is old and clunky. Please use the answer from Adam Boduch for a much more elegant solution.

_(collections) .keyBy('id') // or .indexBy() if using lodash 3.x .at(ids) .value();