Last active
December 15, 2015 21:38
-
-
Save fengdi/5326735 to your computer and use it in GitHub Desktop.
根据路径获取json对象的值 忽略异常返回值友好
This file contains 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 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