我建议您使用两个嵌套的 for 循环,一个用于行,另一个用于列。这是我将如何做的一个例子:const columns = 4;const rows = 4;//if you want to just console.log each number on a different linefor (let i = 1; i <= rows; i++) { for (let j = 1; j <= columns; j++) { console.log(i); } console.log("\n");}//if you want to add each number to an array, and then log the arrayfor (let i = 1; i <= rows; i++) { let columnsArray = []; columnsArray.length = columns; columnsArray.fill(i); console.log(columnsArray);}//if you want to just log the numbers, you can spread the arrayfor (let i = 1; i <= rows; i++) { let columnsArray = []; columnsArray.length = columns; columnsArray.fill(i); console.log(...columnsArray);}//or you could push the arrays in another one, and get a matrix!const matrix = [];for (let i = 1; i <= rows; i++) { let columnsArray = []; columnsArray.length = columns; columnsArray.fill(i); matrix.push(columnsArray);}console.log(matrix);不清楚你想要的输出,所以我有点偏离主题,并为我想到的不同情况做了一个例子。