如何使用alert()查看JavaScript中的数组结构?

时间:2022-03-14 06:45:27

How can I view the structure of an array in JavaScript using alert()?

如何使用alert()在JavaScript中查看数组的结构?

10 个解决方案

#1


114  

A very basic approach is alert(arrayObj.join('\n')), which will display each array element in a row.

一个非常基本的方法是alert(arrayObj.join('\ n')),它将在一行中显示每个数组元素。

#2


51  

EDIT: Firefox and Google Chrome now have a built-in JSON object, so you can just say alert(JSON.stringify(myArray)) without needing to use a jQuery plugin. This is not part of the Javascript language spec, so you shouldn't rely on the JSON object being present in all browsers, but for debugging purposes it's incredibly useful.

编辑:Firefox和谷歌Chrome现在有一个内置的JSON对象,所以你可以说警告(JSON.stringify(myArray))而不需要使用jQuery插件。这不是Javascript语言规范的一部分,因此您不应该依赖于所有浏览器中存在的JSON对象,但出于调试目的,它非常有用。

I tend to use the jQuery-json plugin as follows:

我倾向于使用jQuery-json插件,如下所示:

alert( $.toJSON(myArray) );

This prints the array in a format like

这将以类似的格式打印数组

[5, 6, 7, 11]

However, for debugging your Javascript code, I highly recommend Firebug It actually comes with a Javascript console, so you can type out Javascript code for any page and see the results. Things like arrays are already printed in the human-readable form used above.

但是,为了调试您的Javascript代码,我强烈推荐Firebug它实际上附带了一个Javascript控制台,因此您可以为任何页面键入Javascript代码并查看结果。数组之类的东西已经以上面使用的人类可读形式打印出来。

Firebug also has a debugger, as well as screens for helping you view and debug your HTML and CSS.

Firebug还有一个调试器,以及帮助您查看和调试HTML和CSS的屏幕。

#3


32  

pass your js array to the function below and it will do the same as php print_r() function

将你的js数组传递给下面的函数,它将与php print_r()函数一样

 alert(print_r(your array));  //call it like this

function print_r(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<level+1;j++) level_padding += "    ";

if(typeof(arr) == 'object') { //Array/Hashes/Objects 
    for(var item in arr) {
        var value = arr[item];

        if(typeof(value) == 'object') { //If it is an array,
            dumped_text += level_padding + "'" + item + "' ...\n";
            dumped_text += print_r(value,level+1);
        } else {
            dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
        }
    }
} else { //Stings/Chars/Numbers etc.
    dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
}
return dumped_text;
}

#4


16  

You can use alert(arrayObj.toSource());

你可以使用alert(arrayObj.toSource());

#5


7  

I'd recommend using toString().

我建议使用toString()。

Ex. alert(array.toString()), or console.log(array.toString())

防爆。 alert(array.toString())或console.log(array.toString())

#6


3  

If this is for debugging purposes, I would advise you use a JavaScript debugger such as Firebug. It will let you view the entire contents of arrays and much more, including modifying array entries and stepping through code.

如果这是出于调试目的,我建议您使用一个JavaScript调试器,如Firebug。它将允许您查看数组的全部内容以及更多内容,包括修改数组条目和单步执行代码。

#7


2  

If what you want is to show with an alert() the content of an array of objects, i recomend you to define in the object the method toString() so with a simple alert(MyArray); the full content of the array will be shown in the alert.

如果您想要的是使用alert()显示对象数组的内容,我建议您在对象中定义方法toString(),以便使用简单的警报(MyArray);数组的完整内容将显示在警报中。

Here is an example:

这是一个例子:

//-------------------------------------------------------------------
// Defininf the Point object
function Point(CoordenadaX, CoordenadaY) {
    // Sets the point coordinates depending on the parameters defined
    switch (arguments.length) {
        case 0:
            this.x = null;
            this.y = null;
            break;
        case 1:
            this.x = CoordenadaX;
            this.y = null;
            break;
        case 2:
            this.x = CoordenadaX;
            this.y = CoordenadaY;
            break;
    }
    // This adds the toString Method to the point object so the 
    // point can be printed using alert();
    this.toString = function() {
        return " (" + this.x + "," + this.y + ") ";
    };
 }

Then if you have an array of points:

然后,如果你有一个点数组:

var MyArray = [];
MyArray.push ( new Point(5,6) );
MyArray.push ( new Point(7,9) );

You can print simply calling:

您可以打印简单地打电话:

alert(MyArray);

Hope this helps!

希望这可以帮助!

#8


1  

You could write a function that will convert and format this array as string. Even better: use FireBug for debugging instead of alerts.

您可以编写一个函数,将该数组转换并格式化为字符串。更好的是:使用FireBug进行调试而不是警报。

#9


1  

Better use Firebug (chrome console etc) and use console.dir()

更好地使用Firebug(chrome控制台等)并使用console.dir()

#10


-4  

alert($("#form_id").serialize());

#1


114  

A very basic approach is alert(arrayObj.join('\n')), which will display each array element in a row.

一个非常基本的方法是alert(arrayObj.join('\ n')),它将在一行中显示每个数组元素。

#2


51  

EDIT: Firefox and Google Chrome now have a built-in JSON object, so you can just say alert(JSON.stringify(myArray)) without needing to use a jQuery plugin. This is not part of the Javascript language spec, so you shouldn't rely on the JSON object being present in all browsers, but for debugging purposes it's incredibly useful.

编辑:Firefox和谷歌Chrome现在有一个内置的JSON对象,所以你可以说警告(JSON.stringify(myArray))而不需要使用jQuery插件。这不是Javascript语言规范的一部分,因此您不应该依赖于所有浏览器中存在的JSON对象,但出于调试目的,它非常有用。

I tend to use the jQuery-json plugin as follows:

我倾向于使用jQuery-json插件,如下所示:

alert( $.toJSON(myArray) );

This prints the array in a format like

这将以类似的格式打印数组

[5, 6, 7, 11]

However, for debugging your Javascript code, I highly recommend Firebug It actually comes with a Javascript console, so you can type out Javascript code for any page and see the results. Things like arrays are already printed in the human-readable form used above.

但是,为了调试您的Javascript代码,我强烈推荐Firebug它实际上附带了一个Javascript控制台,因此您可以为任何页面键入Javascript代码并查看结果。数组之类的东西已经以上面使用的人类可读形式打印出来。

Firebug also has a debugger, as well as screens for helping you view and debug your HTML and CSS.

Firebug还有一个调试器,以及帮助您查看和调试HTML和CSS的屏幕。

#3


32  

pass your js array to the function below and it will do the same as php print_r() function

将你的js数组传递给下面的函数,它将与php print_r()函数一样

 alert(print_r(your array));  //call it like this

function print_r(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<level+1;j++) level_padding += "    ";

if(typeof(arr) == 'object') { //Array/Hashes/Objects 
    for(var item in arr) {
        var value = arr[item];

        if(typeof(value) == 'object') { //If it is an array,
            dumped_text += level_padding + "'" + item + "' ...\n";
            dumped_text += print_r(value,level+1);
        } else {
            dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
        }
    }
} else { //Stings/Chars/Numbers etc.
    dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
}
return dumped_text;
}

#4


16  

You can use alert(arrayObj.toSource());

你可以使用alert(arrayObj.toSource());

#5


7  

I'd recommend using toString().

我建议使用toString()。

Ex. alert(array.toString()), or console.log(array.toString())

防爆。 alert(array.toString())或console.log(array.toString())

#6


3  

If this is for debugging purposes, I would advise you use a JavaScript debugger such as Firebug. It will let you view the entire contents of arrays and much more, including modifying array entries and stepping through code.

如果这是出于调试目的,我建议您使用一个JavaScript调试器,如Firebug。它将允许您查看数组的全部内容以及更多内容,包括修改数组条目和单步执行代码。

#7


2  

If what you want is to show with an alert() the content of an array of objects, i recomend you to define in the object the method toString() so with a simple alert(MyArray); the full content of the array will be shown in the alert.

如果您想要的是使用alert()显示对象数组的内容,我建议您在对象中定义方法toString(),以便使用简单的警报(MyArray);数组的完整内容将显示在警报中。

Here is an example:

这是一个例子:

//-------------------------------------------------------------------
// Defininf the Point object
function Point(CoordenadaX, CoordenadaY) {
    // Sets the point coordinates depending on the parameters defined
    switch (arguments.length) {
        case 0:
            this.x = null;
            this.y = null;
            break;
        case 1:
            this.x = CoordenadaX;
            this.y = null;
            break;
        case 2:
            this.x = CoordenadaX;
            this.y = CoordenadaY;
            break;
    }
    // This adds the toString Method to the point object so the 
    // point can be printed using alert();
    this.toString = function() {
        return " (" + this.x + "," + this.y + ") ";
    };
 }

Then if you have an array of points:

然后,如果你有一个点数组:

var MyArray = [];
MyArray.push ( new Point(5,6) );
MyArray.push ( new Point(7,9) );

You can print simply calling:

您可以打印简单地打电话:

alert(MyArray);

Hope this helps!

希望这可以帮助!

#8


1  

You could write a function that will convert and format this array as string. Even better: use FireBug for debugging instead of alerts.

您可以编写一个函数,将该数组转换并格式化为字符串。更好的是:使用FireBug进行调试而不是警报。

#9


1  

Better use Firebug (chrome console etc) and use console.dir()

更好地使用Firebug(chrome控制台等)并使用console.dir()

#10


-4  

alert($("#form_id").serialize());