I understand that it's removing the first 3 elements of the array
and adding them to the new array. But how does the function continue to add ensuing chunks of the array to the new array variable?
How does the while
loop work without proper conditions?
How does it work in collaboration with splice()
here?
function chunkArrayInGroups(arr, size){
let newArr = [];
while(arr.length){
newArr.push(arr.splice(0, size))
}
return newArr;
}
chunkArrayInGroups(["a", "b", "c", "d"], 2);
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…