Skip to content

Instantly share code, notes, and snippets.

@cskevint
Last active December 21, 2015 15:09
Show Gist options
  • Save cskevint/6324739 to your computer and use it in GitHub Desktop.
Save cskevint/6324739 to your computer and use it in GitHub Desktop.
_.pluckNested is a better implementation of _.pluck from underscore.js
// Convenience version of a common use case of `map`: fetching a property.
// The property can be nested using "." notation.
// _.pluckNested([{a:{z:1}},{a:{z:2}}],"a.z") ==> [1, 2]
_.pluckNested = function(obj, key) {
var objKey = key.split('.');
return _.map(obj, function(o) {
var value = o, item;
_.each(objKey, function(key) {
if(_.isObject(value[key])) {
value = value[key];
} else {
item = value[key];
}
});
return item;
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment