Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.56 KB | None | 0 0
  1. var json = {"obj1":{"prop1":{"prop2":{"prop3":"got it"}}}};
  2.  
  3. var prop = getByPath('obj1.prop1.prop2.prop3', json);
  4.  
  5. alert(prop);
  6.  
  7. function getByPath(path, obj){
  8. var resultProp = null;
  9. if(obj && path){
  10. var pathChain = path.split('.');
  11. if(pathChain && pathChain > 0){
  12. var currentProp = obj;
  13. for(var i=0;i<pathChain.length;i++){
  14. if(currentProp[''+pathChain[i]+'']){
  15. currentProp = obj[''+pathChain[i]+'']
  16. }else{
  17. return null;
  18. }
  19. }
  20. return currentProp;
  21. }
  22. }
  23. return resultProp;
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement