Skip to content

Instantly share code, notes, and snippets.

@jeremenichelli
Last active August 29, 2015 14:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeremenichelli/63b75db9434272b16d1d to your computer and use it in GitHub Desktop.
Save jeremenichelli/63b75db9434272b16d1d to your computer and use it in GitHub Desktop.
Given a path, get property of an object
function getFromPath(obj, path, defaultValue) {
var props = path.split('.'),
newObj = obj;
for (var i = 0, len = props.length; i < len; i++) {
if (newObj[props[i]] !== null && newObj[props[i]] !== undefined) {
newObj = newObj[props[i]];
} else {
return defaultValue || null;
}
}
return newObj;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment