If you intend to use this type of template, you can create a mixin as shown below, but it does nothing fundamentally different from your source code. It just makes it more convenient for developers.
_.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 answer is old and awkward. Please use the answer from Adam Boduh for a more elegant solution.
_(collections) .keyBy('id') // or .indexBy() if using lodash 3.x .at(ids) .value();
jessegavin Sep 16 '13 at 14:49 2013-09-16 14:49
source share