Created
December 16, 2014 10:32
-
-
Save nomospace/b4f9e60875d2a5a63dac to your computer and use it in GitHub Desktop.
javascript test for existence of nested object key
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
function checkNested(obj /*, level1, level2, ... levelN*/) { | |
var args = Array.prototype.slice.call(arguments), | |
obj = args.shift(); | |
for (var i = 0; i < args.length; i++) { | |
if (!obj || !obj.hasOwnProperty(args[i])) { | |
return false; | |
} | |
obj = obj[args[i]]; | |
} | |
return true; | |
} | |
var test = {level1:{level2:{level3:'level3'}} }; | |
checkNested(test, 'level1', 'level2', 'level3'); // true | |
checkNested(test, 'level1', 'level2', 'foo'); // false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment