简体   繁体   English

JS console.log 一个二维矩阵

[英]JS console.log a 2d matrix

I have a 2d matrix of arrays:我有一个二维数组矩阵:

let matrix = 
[
[a, b, c], 
[d, e, f],
[g, h, i]
].

How can i print on the console any array on new line and without commas between elements?如何在控制台上在新行上打印任何数组并且元素之间没有逗号? Like this:像这样:

a b c
d e f
g h i

console.log(matrix.join('\\n')) prints any array on new line: console.log(matrix.join('\\n')) 在新行上打印任何数组:

a, b, c
d, e, f
g, h, i

OK, but i don't want commas...好的,但我不想要逗号...

By improving your code, maybe you could modify it like this通过改进你的代码,也许你可以像这样修改它

const printMatrix = matrix.map((d) => d.join(" ")).join("\n")
console.log(printMatrix);

You can use Javascript's console.table() instead of console.log()您可以使用 Javascript 的console.table()而不是 console.log()

This will display your array in table form, making it much more readable and easy to analyse It is a little known feature, however useful when you have a multi-dimentional array.这将以表格形式显示您的数组,使其更具可读性和易于分析。这是一个鲜为人知的功能,但在您拥有多维数组时非常有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM