Created
May 26, 2015 19:11
-
-
Save amit-y/8e3a4608050a7079c166 to your computer and use it in GitHub Desktop.
_ mixin to check for property in deep object
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
_.mixin({ | |
hasProps: function (o, propPath) { | |
var prop = propPath.split('.'); | |
for (var p=0; p<prop.length; p++) { | |
if (!_.has(o, prop[p])) return false; | |
o = o[prop[p]]; | |
} | |
return true; | |
} | |
}); | |
// to test | |
var o = { a: { f: { k: {} } } }; | |
_.hasProps(o,'a.f.k'); //returns true | |
_.hasProps(o,'a.b'); //returns false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment