Skip to content

Instantly share code, notes, and snippets.

@leopku
Created December 18, 2017 12:37
Show Gist options
  • Save leopku/4caedea53273f79eee0745f2d565617e to your computer and use it in GitHub Desktop.
Save leopku/4caedea53273f79eee0745f2d565617e to your computer and use it in GitHub Desktop.
get a value in deep path from an object
<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