Skip to content

Instantly share code, notes, and snippets.

@typeofweb
Created January 22, 2014 18:17
Show Gist options
  • Save typeofweb/8564204 to your computer and use it in GitHub Desktop.
Save typeofweb/8564204 to your computer and use it in GitHub Desktop.
function getProperty (prop, obj) {
var p;
var o = obj;
var props = prop.split('.');
while (props.length) {
p = props.shift();
o = o[p];
if (typeof o === 'undefined') {
break;
}
}
return o;
}
var x = {a:{b:{c:{d:123}}}};
console.log( getProperty('a.b.c', x) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment