Array Chunk v2
Given an array of chunk size, divide the array into many subarrays where each subarray is of length size.
Example:
console.log(chunk([1,2,3,4],2)); => [[1, 2], [3, 4]]
console.log(chunk([1,2,3,4,5],10)) => [[1, 2, 3, 4, 5]]
console.log(chunk([1,2,3,4,5,6,7,8],3)); => [[1, 2, 3], [4, 5, 6], [7, 8]]
console.log(chunk([1,2,3,4,5],2)); = > [[1, 2], [3, 4], [5]]
function chunk(array, size) {
const chunked = [];
let index = 0;
while (index < array.length) {
chunked.push(array.slice(index, index + size));
index += size;
}
return chunked;
}
console.log(chunk([1,2,3,4],2));
console.log(chunk([1,2,3,4,5],10))
console.log(chunk([1,2,3,4,5,6,7,8],3));
console.log(chunk([1,2,3,4,5],2))
}
First we create a function chunk with 2 parameters array and size and first we will declare a new array that is going to hold all these different chunks named chunked = [] and index = 0
Next we will loop with while loop where index is less thant array.length
First we will write slice statement. So we want to slice everything from index to index + size. Array that slice produces an array that contains some number of elements out of the original array.
So we can take this slice that gets produced and just stick it directly into the chunked array by using the push method; by writing chunked.push(array.slice(index, index + size))
Then immediately after that after we do our slice statement right here we need to move on to the next index. So we are going to take our index variable and we’ll add size to it
So we are not incrementing by one here we are incrementing by the size variable because we want essentially take big scoops out of the original array over time
So we say index plus equal size and then remember that one very important step that i would never want you’ll for get makes sure you return chunked at the bottom.