Created
October 6, 2016 18:00
-
-
Save clarketm/534a41927d7298e0b8b38593081e5520 to your computer and use it in GitHub Desktop.
Test for nested JavaScript object key
This file contains hidden or 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
/** | |
* 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