Last active
December 9, 2015 23:49
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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