javascript Console.log 一个多维数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30561056/
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
Console.log a multi-dimensional array
提问by Sam_12345
So I have created a multi-dimensional array: (i.e. one with two sets of 'coordinates')
所以我创建了一个多维数组:(即一个有两组“坐标”)
var items = [[1,2],[3,4],[5,6]];
My site is in development and the array, when it is loaded, and what it contains is constantly changing, so I need to be able to, whilst running the script, dynamically view the contents of the array.
我的网站正在开发中,数组在加载时及其包含的内容不断变化,因此我需要能够在运行脚本的同时动态查看数组的内容。
So, I use this:
所以,我用这个:
console.log(items);
to output it to the console. This gives me the following output:
将其输出到控制台。这给了我以下输出:
So, is there any other way in which I can do the equivalent of console.logging
my array, but with more readableoutput?
那么,有没有其他方法可以使console.logging
我的数组等效于我的数组,但输出更具可读性?
回答by joe_young
You can use javascript's console.table
你可以使用 javascript 的 console.table
This will display your array in table form, making it much more readable and easy to analyse
It is quite a little known feature, however useful when you have a multi-dimentional array.
这将以表格形式显示您的数组,使其更具可读性和易于分析。
这是一个鲜为人知的功能,但在您拥有多维数组时非常有用。
So, change your code to console.table(items);
It should give you something like this:
因此,将您的代码更改为console.table(items);
It 应该会给您以下内容:
----------------------- |(index)| 0 | 1 | | 0 | 1 | 2 | | 1 | 3 | 4 | | 2 | 5 | 6 | -----------------------
回答by Satpal
You can use JSON.stringify()
您可以使用 JSON.stringify()
console.log(JSON.stringify(items));
Its output will be like
它的输出会像
[[1,2],[3,4],[5,6]]
[[1,2],[3,4],[5,6]]
var items = [[1,2],[3,4],[5,6]];
console.log(JSON.stringify(items));