Get All Values in Nested Object Using Recursive Function


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. function lookdeep(obj){
  2. var A= [], tem;
  3. for(var p in obj){
  4. if(obj.hasOwnProperty(p)){
  5. tem= obj[p];
  6. if(tem && typeof tem=='object'){
  7. A[A.length]= p+':{ '+arguments.callee(tem).join(', ')+'}';
  8. }
  9. else A[A.length]= [p+':'+tem.toString()];
  10. }
  11. }
  12. return A;
  13. }
  14.  
  15. var O={a: 1, b: 2, c:{c1: 3, c2: 4, c3:{t:true, f:false}},d: 11};
  16.  
  17. var s=lookdeep(O).join('\n');
  18.  
  19. console.log(s);
  20.  
  21. //Will display
  22. /*
  23. a:1
  24. b:2
  25. c:{ c1:3, c2:4, c3:{ t:true, f:false}}
  26. d:11
  27. */

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.