Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var json = {"obj1":{"prop1":{"prop2":{"prop3":"got it"}}}};
- var prop = getByPath('obj1.prop1.prop2.prop3', json);
- alert(prop);
- function getByPath(path, obj){
- var resultProp = null;
- if(obj && path){
- var pathChain = path.split('.');
- if(pathChain && pathChain > 0){
- var currentProp = obj;
- for(var i=0;i<pathChain.length;i++){
- if(currentProp[''+pathChain[i]+'']){
- currentProp = obj[''+pathChain[i]+'']
- }else{
- return null;
- }
- }
- return currentProp;
- }
- }
- return resultProp;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement