In JS, unlike PHP, it has a print_r (), var_dump () function that prints an array. In order to facilitate day-to-day debugging, we can write a printed function of their own, just like PHP.
Implementation is not difficult, the main use of loops to traverse the array, while using Object.prototype.toString.call to determine if the element is still an array, then recursive execution.
The following is a printed effect chart that you can choose to print with alert, or you can choose to print to the console with Console.log. The printed effect is similar to the Print_r in PHP.
Can be printed to the console
<title>js Print Array Function Example </title>
<script type= "Text/javascript" >
var arr = new Array (' www.111cn.com ');
ARR[1] = new Array (' www ', ' 111cn ', ' com ');
Arrres = Print_arr (arr);
alert (arrres); Eject directly
Console.log (Arrres); Print on console
* @param {[type]} arr the array to print
* @param {[type]} space controls the indentation of printing
* @param {[type]} space2 control print indentation 2
function Print_arr (arr, space, Space2)
var str = "array\n" +space+ "(\ n";
for (var i=0; i<arr.length; i++)
if (Object.prototype.toString.call (arr[i]) = = ' [Object Array] ')
{//Judge whether it is an array, if it is, perform a recursive concatenation
str = space2 + ' [' +i+ '] => ' + Print_arr (arr[i], space+ ', space2+ ');
str = + Space2 + ' [' +i+ '] => ' + arr[i] + ' \ n ';
This function can be placed in the public JS file, the day-to-day work is very convenient to invoke debugging.