Skip to content

Instantly share code, notes, and snippets.

@chetbox
Created January 7, 2016 18:45
Show Gist options
  • Save chetbox/7a21af8e44624b549b18 to your computer and use it in GitHub Desktop.
Save chetbox/7a21af8e44624b549b18 to your computer and use it in GitHub Desktop.
/* Get a value from a nested object using just a string for readability
*
* Example:
* var o = {first: {second: {third: {a: 1, b: 2}}}};
* o.get_in('first.second.third.b'); // => 2
*/
Object.prototype.get_in = function(selector) {
return selector.split('.').reduce(
function(obj, key) {
return obj[key];
},
this
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment