Created
January 7, 2016 18:45
-
-
Save chetbox/7a21af8e44624b549b18 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
/* 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