Skip to content

Instantly share code, notes, and snippets.

@jmorenoamor
Last active March 15, 2020 11:26
Show Gist options
  • Save jmorenoamor/24830632dae49418ae6fffd5990fa18d to your computer and use it in GitHub Desktop.
Save jmorenoamor/24830632dae49418ae6fffd5990fa18d to your computer and use it in GitHub Desktop.
Returns the value of a nested javascript reference path
/*
* Returns the value of a nested javascript reference path
*/
function nestedValue(obj, path) {
return path.replace(/\[(\d+)\]/g, '.$1').split('.').reduce((previous, current) => {
return previous && previous.hasOwnProperty(current) ? previous[current] : null;
}, obj);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment