Created
March 21, 2013 11:33
-
-
Save shiawuen/5212386 to your computer and use it in GitHub Desktop.
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 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