Skip to content

Instantly share code, notes, and snippets.

@ungoldman
Last active August 29, 2015 14:00
Show Gist options
  • Save ungoldman/05c5604d2ed0af5f86af to your computer and use it in GitHub Desktop.
Save ungoldman/05c5604d2ed0af5f86af to your computer and use it in GitHub Desktop.
Get value in object from path as array of keys
var obj = {
a: {
b: {
c: 'yay'
}
}
};
var path = ['a','b','c'];
function valueFromPath (obj, arr) {
var a = arr.slice(0);
var key = a.shift();
if (a.length) {
return valueFromPath(obj[key], a);
}
return obj[key];
}
valueFromPath(obj, path);
// -> 'yay'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment