Last active
March 15, 2020 11:26
-
-
Save jmorenoamor/24830632dae49418ae6fffd5990fa18d to your computer and use it in GitHub Desktop.
Returns the value of a nested javascript reference path
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
/* | |
* 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