Skip to content

Instantly share code, notes, and snippets.

@clarketm
Created October 6, 2016 18:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clarketm/534a41927d7298e0b8b38593081e5520 to your computer and use it in GitHub Desktop.
Save clarketm/534a41927d7298e0b8b38593081e5520 to your computer and use it in GitHub Desktop.
Test for nested JavaScript object key
/**
* Test for nested JavaScript object key
*
* @memberof Object.prototype
* @param {...String} key string(s)
* @return {Boolean} has nested key
*
* [http://stackoverflow.com/questions/2631001/javascript-test-for-existence-of-nested-object-key]
*/
Object.prototype.checkNested = function() {
var self = this,
args = Array.prototype.slice.call(arguments, 0);
for (var i = 0; i < args.length; i++) {
if (!self || !self.hasOwnProperty(args[i])) {
return false;
}
self = self[args[i]];
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment