Skip to content

Instantly share code, notes, and snippets.

@fengdi
Last active December 15, 2015 21:38
Show Gist options
  • Save fengdi/5326735 to your computer and use it in GitHub Desktop.
Save fengdi/5326735 to your computer and use it in GitHub Desktop.
根据路径获取json对象的值 忽略异常返回值友好
function getValueByPath(obj, path){
(path+"").replace(/[^.:]+/g,function(n){
obj = (obj!=void 0 && (typeof obj=="object" || typeof obj=="function") && n in obj) ? obj[n] : void 0;
});
return obj;
}
// getValueByPath({a:[{c:"r"}]}, "a.0.c"); // r
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment