Last active
October 1, 2018 09:27
-
-
Save dastagirkhan/8b0bdaed756edc76138569e5a4891e98 to your computer and use it in GitHub Desktop.
JS: snippets
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 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