var people = [
{firstName : \"Thein\", city : \"ny\", qty : 5},
{firstName : \"Michael\", city : \"ny\", qty : 3},
{firstName : \"Bloom\", city : \"nj\", qty
TL;DR Use :
var results = _.chain(people)
.where({ city: "ny" })
.map(_.partialRight(_.pick, 'firstName', 'qty'))
.value();
But please read on for explanations as I feel the process of finding this solution is more interesting than the actual answer.
The general pattern would be (it works with lodash too) :
_.map(array, function(obj) { return _.pick(obj, 'x', 'y', 'z'); });
Given this general map function which transforms each element of a collection, there are multiple ways to adapt this to your particular situation (that vouch for the flexibility of map
, which is a very basic building block of functional programs).
Let me present below several ways to implement our solution :
var _ = require('lodash'); // @lodash 2.4.1 at the time of writing
// use underscore if you want to, but please see http://stackoverflow.com/questions/13789618/differences-between-lodash-and-underscore
/* la data */
var people = [{
firstName: "Thein",
city: "ny",
qty: 5
}, {
firstName: "Michael",
city: "ny",
qty: 3
}, {
firstName: "Bloom",
city: "nj",
qty: 10
}];
/* OPTION1 : mixin' with _ */
_.mixin({
pluckMany: function() {
var array = arguments[0],
propertiesToPluck = _.rest(arguments, 1);
return _.map(array, function(item) {
/* Alternative implementation 1.1
* ------------------------------
* Taken from @mMcGarnagle answer
* _each is easy to understand here,
* but has to modify the variable `obj` from a closure
* I try to avoid that for trivial cases like this one.
*/
var obj = {};
_.each(propertiesToPluck, function(property) {
obj[property] = item[property];
});
return obj;
/* Alternative implementation 1.2
* ------------------------------
* Rewrite the previous code,
* by passing the accumulator (previously`obj`, but really it is an object that accumulates the result being constructed) across function calls.
* This construction is typical of the `reduce` function, closer to a functionnal programming style.
*/
return _.reduce(propertiesToPluck, function(obj, property) {
obj[property] = item[property];
return obj;
}, {});
/* Alternative implementation 1.3
* ------------------------------
* If we are already using lodash/underscore,
* then let's use the `pick` function ! I also included an example of `flatten` here
*/
return _.pick(item, _.flatten(propertiesToPluck, true));
/* Alternative implementation 1.4
* ------------------------------
* But really flatten is not needed.
*/
return _.partial(_.pick, item).apply(null, propertiesToPluck);
});
}
});
/* Let's use our mixed function !
* Since we call several _ functions on the same object
* it is more readable to chain the calls.
*/
var results = _.chain(people)
.where({
city: "ny"
})
.pluckMany('firstName', 'qty')
.value();
/* OPTION 2 : without mixing our code with lodash/underscore */
var results = _.chain(people)
.where({
city: "ny"
})
.map(_.partialRight(_.pick, 'firstName', 'qty'))
.value();
console.log(results);
If you like this way of writing code with underscore
or lodash
, I highly suggest that you have a look at functional programming, as this style of writing as well as many functions (map
, reduce
amongst many others) come from there.
Note : This is apparently a common question in underscore : https://github.com/jashkenas/underscore/issues/1104
This is apparently no accident if these are left out of underscore/lodash : "composability is better than features". You could also say do one thing and do it well
. This is also why _.mixin
exists.