A print_r function for Javascript (Recursively dumping arrays)

By on

Binny V A over at Bin-Blog has written something marvellous: a print_r function for Javascript.

What does it do? It takes an array (or hash or object) and prints out every key with its value. If it comes along another array, it does the same thing again. It's such a useful feature in PHP, it's a great debugger-friend.

There are a few code snippets out there that do the same job, but I thought this one was quite small and compact. Don't forget to wrap it in a <pre> tag to get a nice layout.

/**
 * Function : dump()
 * Arguments: The data - array,hash(associative array),object
 *    The level - OPTIONAL
 * Returns  : The textual representation of the array.
 * This function was inspired by the print_r function of PHP.
 * This will accept some data as the argument and return a
 * text that will be a more readable version of the
 * array/hash/object that is given.
 * Docs: http://www.openjs.com/scripts/others/dump_function_php_print_r.php
 */
function dump(arr,level) {
  var dumped_text = "";
  if(!level) level = 0;
  
  //The padding given at the beginning of the line.
  var level_padding = "";
  for(var j=0;j \"" + value + "\"\n";
      }
    }
  } else { //Stings/Chars/Numbers etc.
    dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
  }
  return dumped_text;
}

Comments

ospsqumw

01/01/1967

linkiNot

That's amazing! I’m all in, I’d better go to bed now.

MuillNot

Surprise. He is in charge of marketing.

MuillNot

An investment banking firm focused on providing strategic advice and transaction services to owners of middle-market companies. He doesn’t rub shoulders with the rich.

Cheesternab

альбом под монеты [url=http://www.ohotniki.ru/forum/theme1740-podarok-rukovoditelyu.html]альбом нумизмата[/url]

Cheesternab

альбом под монеты [url=http://apologetics.ru/index.php/2013-03-08-15-51-24/70-2013-03-08-15-55-22/3444-2014-08-29-15-35-08]монетный альбом[/url]

Besternab

Подскажите, что и где ? -альбом для монет [url=http://spartak70.ru/history/1991-1995/381.html]10 рублевки РФ[/url]

đồ gỗ óc chó

Excellent, what a webpage it is! This web site gives valuable facts to us, keep it up.

http://shorethingcafe.com/on-line-casino-grand-bay-welcomes-worldwide-gamers

Looking to play at safe Irish online casino sites?

CABLES & CHARGERS

Fibre optic cabling won't amount to much, and they are easy to install. Even with the advanced technology it's taken a while for that public to take the DVD. Furthermore, wavelength division multiplexing enabled the fiber optic cables to handle massive numbers of data.

color printer

In these days of intense competition, an online printer just will not be able to stay in business if he or she is producing sub-standard products. In addition, you can be able to attain free lamination from your expected printing firm. This is your networking or promoting of one's business and contains to be good, first impressions are necessary to your business and also the business card is a great method to generate prospective clients, and they will judge it.

how to get help in windows 10

Superb, what a website it is! This weblog gives useful information to us, keep it up.

rardWoofe

hi :) bross :)

Jelle De Loecker