Skip to content

Instantly share code, notes, and snippets.

@wejrowski
Last active October 28, 2015 19:10
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 wejrowski/a407fe384e3af9c3356d to your computer and use it in GitHub Desktop.
Save wejrowski/a407fe384e3af9c3356d to your computer and use it in GitHub Desktop.
Get path value
// via http://www.redotheweb.com/2015/09/18/declarative-imperative-js.html
function getValue(object, propertyName) {
if (!propertyName) throw new Error('Impossible to set null property');
return typeof object === 'undefined' ? undefined : object[propertyName];
}
function getNestedValue(object, propertyName) {
return propertyName.split('.').reduce(getValue, object);
}
var myObj = {
x: "hi",
john: {
first: "j",
last: "doe",
address: {
street: "103 10th st",
state: "AZ"
}
},
bill: {
first: "bill",
last: "johnson"
}
};
getNestedValue(myObj, "john.address.street");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment