Useful Javascript

From Triangle Wiki

Jump to: navigation, search

Javascript Var_Dump

  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.  
  21. for(var item in arr) {
  22. var value = arr[item];
  23.  
  24. if(typeof(value) == 'object') { //If it is an array,
  25. dumped_text += level_padding + "'" + item + "' ...\n";
  26. dumped_text += dump(value,level+1);
  27. } else {
  28. dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
  29. }
  30. }
  31.  
  32. } else { //Stings/Chars/Numbers etc.
  33. dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
  34. }
  35.  
  36. return dumped_text;
  37. }

Navigation

Edit Navigation


Personal tools