Created
September 14, 2014 14:02
-
-
Save thejhh/bf8744e1df3d32b7f22b to your computer and use it in GitHub Desktop.
This file contains hidden or 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 is_object(a) { | |
| return a && (typeof a === 'object'); | |
| } | |
| function step(last, current) { | |
| if(is_object(last)) { | |
| return last[current]; | |
| } | |
| } | |
| function get_property(obj, path) { | |
| return path.split('.').reduce(step, obj); | |
| } | |
| /* | |
| * > get_property({'foo':{'bar': 1}}, 'foo.bar') | |
| * 1 | |
| * > get_property({'foo':{'bar': 1}}, 'foo') | |
| * { bar: 1 } | |
| * > get_property({'foo':{'bar': 1}}, 'foo.bar.hello') | |
| * undefined | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment