Created
October 5, 2012 11:13
-
-
Save emilisto/3839275 to your computer and use it in GitHub Desktop.
pickAll: tiny underscore extension by @svammel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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