I typically just use uneval() to figure out what’s inside an array/object, but what about when it’s large and heterogeneous? I wanted to find a version of php’s print_r() for JavaScript. Here is link to the Original Version of dump() I based mine off of:
www.openjs.com/scripts/others/dump_function_php_print_r.php
When I tried it, the first thing I noticed what that it put quotes around everything, and that long strings with line breaks got messy, so I did a quick adaptation to suit my immediate needs and came up with this:
function dump(arr,level) { function magicquotes(value) { return (isNaN(value)) ? '"' + value.replace(/\n/g,"\n"+indent) + '"' : value; } level = level | 0; var indent = new Array(level+1).join("\t"), dumped_text = ""; if(typeof(arr) == 'object') { //Array/Hashes/Objects for(var item in arr) { var value = arr[item]; if(typeof(value) == 'object') dumped_text += indent + "'" + item + "' :\n" + dump(value,level+1); else dumped_text += indent + "'" + item + "' => " + magicquotes(value) + "\n"; } } else { //Stings/Chars/Numbers etc. dumped_text = "===>"+arr+"<===("+typeof(arr)+")"; } return dumped_text; }
I also found links to many other print_r(), var_dump() equivalents, but they either depended on write, were overly complicated, or returned a lot of excess text I wasn’t interested in.
So why not just call it print_r ? Well, the original I copied was called dump, and I’ve always been annoyed typing that underscore, so I just didn’t change it.
Update: found another alternative. It’s really long and puts out a lot of extraneous information, but it’s worth looking at: wiki.greasespot.net/Code_snippets#Dump_the_properties_of_an_object