Created
December 18, 2017 12:37
-
-
Save leopku/4caedea53273f79eee0745f2d565617e to your computer and use it in GitHub Desktop.
get a value in deep path from an object
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
| <script id="jsbin-javascript"> | |
| let r = document.getElementsByTagName('script')[0]; | |
| // console.log(r); | |
| function getNested (theObject, path, defaultValue) { | |
| try { | |
| separator = '.'; | |
| return path. | |
| replace('[', separator).replace(']',''). | |
| split(separator). | |
| reduce( | |
| function (obj, property) { | |
| return obj[property]; | |
| }, theObject | |
| ); | |
| } catch (err) { | |
| return defaultValue ? defaultValue : undefined; | |
| } | |
| } | |
| let firstChildNode = getNested(r, 'childNodes.1.ownerDocument.activeElement', {}); | |
| console.log(firstChildNode); | |
| </script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment