Posted By

BFTrick on 05/24/10


Tagged

javascript array function dump


Versions (?)

dump array


 / Published in: JavaScript
 

  1. /**
  2. * Function : dump()
  3. * Arguments: The data - array,hash(associative array),object
  4. * The level - OPTIONAL
  5. * Returns : The textual representation of the array.
  6. * This function was inspired by the print_r function of PHP.
  7. * This will accept some data as the argument and return a
  8. * text that will be a more readable version of the
  9. * array/hash/object that is given.
  10. */
  11. function dump(arr,level) {
  12. var dumped_text = "";
  13. if(!level) level = 0;
  14.  
  15. //The padding given at the beginning of the line.
  16. var level_padding = "";
  17. for(var j=0;j<level+1;j++) level_padding += " ";
  18.  
  19. if(typeof(arr) == 'object') { //Array/Hashes/Objects
  20. for(var item in arr) {
  21. var value = arr[item];
  22.  
  23. if(typeof(value) == 'object') { //If it is an array,
  24. dumped_text += level_padding + "'" + item + "' ...\n";
  25. dumped_text += dump(value,level+1);
  26. } else {
  27. dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
  28. }
  29. }
  30. } else { //Stings/Chars/Numbers etc.
  31. dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
  32. }
  33. return dumped_text;
  34. }

Report this snippet  

You need to login to post a comment.