Last active
October 29, 2015 10:08
-
-
Save rexso/6484fa7fbe2247b73b50 to your computer and use it in GitHub Desktop.
JavaScript function used solve a string path of an Object
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 valueFromPath(object, path, separator) { | |
if("object" == typeof object && "string" == typeof path) { | |
var node = object; | |
if("string" != typeof separator || !separator.length) { | |
separator = "/"; | |
} | |
for(var segment; path && (segment = path.split(separator)[0]); path = path.slice(segment.length + separator.length)) { | |
if("object" == typeof node) { | |
node = node[segment]; | |
} | |
} | |
return node; | |
} | |
return undefined; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Improvedversion: