Skip to content

Instantly share code, notes, and snippets.

@emilisto
Created October 5, 2012 11:13
Show Gist options
  • Save emilisto/3839275 to your computer and use it in GitHub Desktop.
Save emilisto/3839275 to your computer and use it in GitHub Desktop.
pickAll: tiny underscore extension by @svammel
/*
* pickAll: tiny underscore extension by @svammel
*
* Example:
*
* var users = [
* { "user": "john", "email": "[email protected]", "url": "www.john.com" },
* { "user": "pete", "email": "[email protected]", "url": "www.pete.com" }
* ];
*
* _.pickAll(users, 'user', 'email');
*
* Will return the same array but with only the fields 'user' and 'email' for
* each element.
*
*/
if(typeof _ === 'undefined') throw "requires underscore";
_.pickAll = function(arr) {
var args = Array.prototype.slice.call(arguments, 1);
return _.map(arr, function(el) {
return _.pick.apply(null, [ el ].concat(args));
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment