Advertisement
Guest User

Untitled

a guest
Jul 29th, 2015
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. //your object
  2. var o = {
  3. foo:"bar",
  4. arr:[1,2,3],
  5. subo: {
  6. foo2:"bar2"
  7. }
  8. };
  9.  
  10. //called with every property and it's value
  11. function process(key,value) {
  12. log(key + " : "+value);
  13. }
  14.  
  15. function traverse(o,func) {
  16. for (i in o) {
  17. func.apply(this,[i,o[i]]);
  18. if (typeof(o[i])=="object") {
  19. //going on step down in the object tree!!
  20. traverse(o[i],func);
  21. }
  22. }
  23.  
  24. //that's all... no magic, no bloated framework
  25. traverse(o,process);
  26.  
  27.  
  28.  
  29.  
  30. function traverse(jsonObj) {
  31. if( typeof jsonObj == "object" ) {
  32. $.each(jsonObj, function(k,v) {
  33. // k is either an array index or object key
  34. traverse(v);
  35. }
  36. }
  37. else {
  38. // jsonOb is a number or string
  39. }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement