如何使用 alert() 在 JavaScript 中查看数组结构?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3006644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I view array structure in JavaScript with alert()?
提问by pppttt
How can I view the structure of an array in JavaScript using alert()?
如何使用 JavaScript 查看数组的结构alert()?
回答by Humberto
A very basic approach is alert(arrayObj.join('\n')), which will display each array element in a row.
一个非常基本的方法是alert(arrayObj.join('\n')),它将显示一行中的每个数组元素。
回答by Eli Courtwright
EDIT:Firefox and Google Chrome now have a built-in JSONobject, 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 JSONobject being present in all browsers, but for debugging purposes it's incredibly useful.
编辑:Firefox 和 Google Chrome 现在有一个内置JSON对象,所以你可以说alert(JSON.stringify(myArray))不需要使用 jQuery 插件。这不是 Javascript 语言规范的一部分,因此您不应该依赖JSON所有浏览器中都存在的对象,但出于调试目的,它非常有用。
I tend to use the jQuery-json pluginas 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 highlyrecommend FirebugIt 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 的屏幕。
回答by Fawad Ghafoor
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;
}
回答by Piseth Sok
You can use alert(arrayObj.toSource());
您可以使用 alert(arrayObj.toSource());
回答by dk123
I'd recommend using toString().
我建议使用 toString()。
Ex. alert(array.toString()), or console.log(array.toString())
前任。alert(array.toString()), 或者console.log(array.toString())
回答by Justin Ethier
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。它将让您查看数组的全部内容以及更多内容,包括修改数组条目和单步执行代码。
回答by tomasofen
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() 方法,以便使用简单的 alert(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!
希望这可以帮助!
回答by Victor S.
For readability purposes you can use:
出于可读性目的,您可以使用:
alert(JSON.stringify(someArrayOrObj, '', 2));
alert(JSON.stringify(someArrayOrObj, '', 2));
More about JSON.stringify().
更多关于JSON.stringify()。
Example:
例子:
let user = {
name: "John",
age: 30,
roles: {
isAdmin: false,
isEditor: true
}
};
alert(JSON.stringify(user, "", 2));
/* Result:
{
"name": "John",
"age": 30,
"roles": {
"isAdmin": false,
"isEditor": true
}
}
*/
回答by Darin Dimitrov
回答by yAnTar
Better use Firebug (chrome console etc) and use console.dir()
更好地使用 Firebug(chrome 控制台等)并使用 console.dir()

