Cold Fusion Tips-N-Tricks
Dumping a Javascript array

If you have a large multidimensional array, debugging your Javascript could be about as fun as finding out that your computer just infected the entire corporate network with a virus and now everybody is giving you funny looks.

I found a set of Javascript functions that will dump out an entire Javascript array into a series of lists.  However, being the ColdFusion geek that I am, I modified it so that the Javascript dump looks somewhat like the results produced by the <cfdump> tag.

As an added benefit, the script comes with functions to allow for the easy addition and deletion of rows from an array.  Click here to download.

Take the following Javascript code:

<script language="Javascript">
var tmpArray=new Array(1);
tmpArray[0]='a';
tmpArray[1]=new Array(1);
tmpArray[1][0]='b';
tmpArray[1][1]=new Array(1);
tmpArray[1][1][0]='c';
tmpArray[1][1][1]='d';
tmpArray[1][2]='e';
tmpArray[1][3]=new Array('f','g');
tmpArray[1][3][2]=new Array('h','i','j','k');
tmpArray[1][4]='l';
tmpArray[2]='m';
</script>

Now if I make a call to the function dumpArrayValues, it displays the table.

<script language="Javascript">
dumpArrayValues(tmpArray);
</script>




The original function, renamed to dumpArrayValuesList, would return the dump as below:

<script language="Javascript">
dumpArrayValuesList(tmpArray);
</script>



Return to the Cold Fusion Tips-N-Tricks topic list