Skip to content

Instantly share code, notes, and snippets.

@humphd
Created September 16, 2013 19:02
Show Gist options
  • Select an option

  • Save humphd/6584999 to your computer and use it in GitHub Desktop.

Select an option

Save humphd/6584999 to your computer and use it in GitHub Desktop.
safeLookup of a JSON inflated object along a path
function safeLookup(obj, propPath, fallback) {
fallback = fallback || '';
var props = propPath.split('.');
// Descend obj along the path specified in propPath, but
// don't trust that that path is good, and may contain
// undefined routes.
try {
props.forEach(function(prop) {
obj = obj[prop];
});
return obj || fallback;
} catch (e) {
return fallback;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment