Skip to content

Instantly share code, notes, and snippets.

@stpe
Created September 9, 2015 09:47
Show Gist options
  • Save stpe/1e07483e8ee0b902e7a3 to your computer and use it in GitHub Desktop.
Save stpe/1e07483e8ee0b902e7a3 to your computer and use it in GitHub Desktop.
Get nested object properties (just another take on implementation)
function get(obj, path) {
var parts = path.split(/\]\[|\]\.|\[|\]|\./);
return parts
.reduce(function(o, x) {
return (typeof o == "undefined" || o === null) ? x : o[x];
}, obj);
}
// example: get(obj, "prop.foo[0][2].bar[3].zoom")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment