Skip to content

Instantly share code, notes, and snippets.

@doublejosh
Last active May 15, 2016 08:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doublejosh/f63e28a24ba9a9d9d787b46a6d19e834 to your computer and use it in GitHub Desktop.
Save doublejosh/f63e28a24ba9a9d9d787b46a6d19e834 to your computer and use it in GitHub Desktop.
/**
* Reach nested data object properties via string.
*
* @param {object} obj
* @param {string} desc
*
* @return {mixed}
*/
_.strProp = function (obj, desc) {
var arr = desc.split('.');
while (arr.length && (obj = obj[arr.shift()]));
return obj;
};
/**
* Example...
*/
console.log(_.strProp({a: {j: 1, b: 2}, c: 3}, 'a.b'));
//=> 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment