For loop For循环与块数组练习
我写了这段代码,有人能解释一下在每次迭代中I是如何递增的吗For loop For循环与块数组练习,for-loop,For Loop,我写了这段代码,有人能解释一下在每次迭代中I是如何递增的吗 function chunk(array, size) { let chunked = []; for (i = 0; i < array.length;) { chunked.push(array.splice(i, size)); } return chunked; } let a = chunk([1, 2, 3, 4, 5, 6, 7, 8], 2); console.log(a); 功能块
function chunk(array, size) {
let chunked = [];
for (i = 0; i < array.length;) {
chunked.push(array.splice(i, size));
}
return chunked;
}
let a = chunk([1, 2, 3, 4, 5, 6, 7, 8], 2);
console.log(a);
功能块(数组、大小){
设chunked=[];
对于(i=0;i
您的“array.splice”从数组中删除“size”元素。
size是来自chunk函数的参数
这意味着在每次迭代之后,array.length的“size”变短。
当数组包含0个元素时,循环结束
在您的示例中,chunk函数在每个迭代步骤中从数组中删除2个元素