Last active
October 27, 2017 03:38
-
-
Save cubicleWar/d8855d2c49e6584f5561 to your computer and use it in GitHub Desktop.
Read nested a nested property from an object given a key sequence string like 'location.x.elevation'
This file contains 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 readNested(obj, keys) | |
{ | |
var parts = keys.split('.'), | |
curr = obj; | |
for (var i = 0; i < parts.length; i++) | |
{ | |
if (!curr[parts[i]]) | |
{ | |
return null; | |
} | |
curr = curr[parts[i]]; | |
} | |
return curr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment