Skip to content

Instantly share code, notes, and snippets.

@amit-y
Created May 26, 2015 19:11
Show Gist options
  • Save amit-y/8e3a4608050a7079c166 to your computer and use it in GitHub Desktop.
Save amit-y/8e3a4608050a7079c166 to your computer and use it in GitHub Desktop.
_ mixin to check for property in deep object
_.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