Skip to content

Instantly share code, notes, and snippets.

@dastagirkhan
Last active October 1, 2018 09:27
Show Gist options
  • Select an option

  • Save dastagirkhan/8b0bdaed756edc76138569e5a4891e98 to your computer and use it in GitHub Desktop.

Select an option

Save dastagirkhan/8b0bdaed756edc76138569e5a4891e98 to your computer and use it in GitHub Desktop.
JS: snippets
// Test for existence of nested JavaScript object key
function checkNested(obj) {
var args = Array.prototype.slice.call(arguments, 1);
for (var i = 0; i < args.length; i++) {
if (!obj || !obj.hasOwnProperty(args[i])) {
return false;
}
obj = obj[args[i]];
}
return true;
}
// Testing nested objects as undefined in Javascript
function tryGetPropertyValue(o, propertyName1 /*, ... propertyNameN */) {
var names = [].slice.call(arguments, 1);
while (o && names.length) {
o = o[names.shift()];
}
return names.length ? null : o;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment