如何使数组的输出中的下一行从上一个数组继续



如何获得数组输出中的下一行以继续上一个数组?这是我迄今为止尝试过的

let myArray = ([[1],[2],[3],[4],[5],[6],[7]])
for(var i = 1; i<=5; i++){
myArray.map(i => ++i);
console.log(myArray)
}

这是我尝试过的代码的输出:

[[1], [2], [3], [4], [5], [6], [7]]
[[1], [2], [3], [4], [5], [6], [7]]
[[1], [2], [3], [4], [5], [6], [7]]
[[1], [2], [3], [4], [5], [6], [7]]
[[1], [2], [3], [4], [5], [6], [7]]
[[1], [2], [3], [4], [5], [6], [7]]
[[1], [2], [3], [4], [5], [6], [7]]
[[1], [2], [3], [4], [5], [6], [7]]

但这是我想要的解决方案:

[[1], [2], [3], [4], [5], [6], [7]]
[[8], [9], [10], [11], [12], [13], [14]]
[[15], [16], [17], [18], [19], [20], [21]]
[[22], [23], [24], [25], [26], [27], [28]]
[[29], [30]

我是新手,所以如果我的简单解决方案出错,请对我宽容一点。

map((方法创建一个新数组,其中填充了调用所提供函数的结果。因此,您必须将值重新分配回变量。

let myArray = ([[1],[2],[3],[4],[5],[6],[7]])
for(var i = 1; i<=5; i++){
console.log(myArray)
// create a new array and map it to the array item
myArray = myArray.map(i => [i[0] + myArray.length]);
}

最新更新