Skip to content

Instantly share code, notes, and snippets.

@shiawuen
Created March 21, 2013 11:33
Show Gist options
  • Save shiawuen/5212386 to your computer and use it in GitHub Desktop.
Save shiawuen/5212386 to your computer and use it in GitHub Desktop.
function lookup(obj /*Object */, props/* String */){
if ( ! (obj && props)) { return undefined; }
var ps = props.split('.');
var pointer = obj;
var val = undefined;
var i = 0, len = ps.length;
for(;i<len;i++) {
pointer = pointer[ps[i]];
if ('undefined' === typeof pointer) {
break;
}
}
if (i === len) { val = pointer; }
return val;
}
lookup({a:{b:{c:'a'}}}, 'a.b.c'); // == a
lookup({a:{b:{c:'a'}}}, 'a.b.e'); // == undefined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment