Last active
August 29, 2015 14:01
-
-
Save onexdrk/0b0b84fe6f0c8f0a895b to your computer and use it in GitHub Desktop.
underscore.js mixin for pick nested properties
This file contains 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
/** | |
* Require _.deep.js from https://gist.github.com/furf/3208381 | |
*/ | |
_.mixin({ | |
deepPick: function (obj) { | |
var ArrayProto = Array.prototype; | |
var copy = {}; | |
var keys = ArrayProto.concat.apply(ArrayProto, ArrayProto.slice.call(arguments, 1)); | |
_.each(keys, function(key) { | |
var val = _.deep(obj, key); | |
if (!_.isUndefined(val)) _.deep(copy, key, val); | |
}); | |
return copy; | |
} | |
}); | |
//Usage: | |
// var obj = { | |
// a : { | |
// b : 'b', | |
// c:'c' | |
// }, | |
// x:{ | |
// y:'y', | |
// z:'z' | |
// } | |
// }; | |
// | |
//_.deepPick(z, 'a.b','x.z'); | |
// | |
// result: | |
// { | |
// a: { | |
// b:'b' | |
// }, | |
// x: { | |
// z:'z' | |
// } | |
//} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think it needs a small modification, because some Falsy values will be not included. For example:
Solve this problem by changing line 13 to:
Now the example again: