Last active
August 29, 2015 14:07
-
-
Save Amberlamps/1132ef4f5757754964e6 to your computer and use it in GitHub Desktop.
Resolving dot separated key fields in nested Javascript objects.
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 resolveKeyString(obj, keyString) { | |
return keyString.split('.').reduce(function(p, c) { | |
return p[c]; | |
}, t); | |
} | |
// Usage: | |
var obj = { | |
a: { | |
b: { | |
c: 'value' | |
} | |
} | |
}; | |
var keyString = 'a.b.c'; | |
console.log(resolveKeyString(obj, keyString)); | |
// => 'value' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment