Skip to content

Instantly share code, notes, and snippets.

@danieldai
Created February 1, 2021 09:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danieldai/2754e8618c0c2aad7dbf6dc0c4a6e4f0 to your computer and use it in GitHub Desktop.
Save danieldai/2754e8618c0c2aad7dbf6dc0c4a6e4f0 to your computer and use it in GitHub Desktop.
Get nested property of a JavaScript object
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