Skip to content

Instantly share code, notes, and snippets.

@niyazpk
Last active December 9, 2015 23:49
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 niyazpk/4346601 to your computer and use it in GitHub Desktop.
Save niyazpk/4346601 to your computer and use it in GitHub Desktop.
extract(object, string) : Uses the string as a path finder inside the object to retrieve the specified item. Returns undefined if anything went wrong in finding the item.
function extract(obj, path) {
try {
return eval("obj." + path);
} catch(e) {
return undefined;
}
}
var obj = {
user: {
order: {
address : {
city: 'Bangalore'
}
}
}
}
extract(obj, 'user.order.address.city');
// 'Bangalore'
extract(obj, 'user.home.address.city');
// undefined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment