Skip to content

Instantly share code, notes, and snippets.

@andreyshuster
Created December 10, 2013 09:01
Show Gist options
  • Save andreyshuster/7887657 to your computer and use it in GitHub Desktop.
Save andreyshuster/7887657 to your computer and use it in GitHub Desktop.
get object value by passing string
Object.byString = function(o, s) {
s = s.replace(/\[(\w+)\]/g, '.$1'); // convert indexes to properties
s = s.replace(/^\./, ''); // strip a leading dot
var a = s.split('.');
while (a.length) {
var n = a.shift();
if (n in o) {
o = o[n];
} else {
return;
}
}
return o;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment