Skip to content

Instantly share code, notes, and snippets.

@aaronmccall
Last active December 16, 2015 16:19
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 aaronmccall/5462344 to your computer and use it in GitHub Desktop.
Save aaronmccall/5462344 to your computer and use it in GitHub Desktop.
Eliminates the need to do obj && obj.prop && obj.prop.subprop testing
function resolver(prop_string, obj) {
if (!obj) return;
if (prop_string.indexOf('.') === -1) return obj[prop_string];
var props = prop_string.split('.'),
p = props.length,
result = obj,
i = 0,
prop;
for (; i<p; i++) {
prop = props[i];
result = result[prop];
if (result === undefined) return result;
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment