Last active
August 29, 2015 14:00
-
-
Save ungoldman/05c5604d2ed0af5f86af to your computer and use it in GitHub Desktop.
Get value in object from path as array of keys
This file contains hidden or 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
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