Skip to content

Instantly share code, notes, and snippets.

@thejhh
Created September 14, 2014 14:02
Show Gist options
  • Select an option

  • Save thejhh/bf8744e1df3d32b7f22b to your computer and use it in GitHub Desktop.

Select an option

Save thejhh/bf8744e1df3d32b7f22b to your computer and use it in GitHub Desktop.
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