-
-
Save danieldai/2754e8618c0c2aad7dbf6dc0c4a6e4f0 to your computer and use it in GitHub Desktop.
Get nested property of a JavaScript 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
function getNestedProperty(obj, path) { | |
let myObj = obj; | |
for (let name of path.split('.')) { | |
if(myObj) { | |
myObj = myObj[name]; | |
} else { | |
break; | |
} | |
} | |
return myObj; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment