By array chunking, I mean taking an entire array and creating one array containing smaller subarrays of the original array's elements. Not only is this a commonly asked concept on technical interviews, but I can also see how there may be use cases for it when ordering or organizing a dataset.
Here I will tackle a relatively simple "array chunking" problem and go through a few different ways of solving it. By no means are these the only ways of doing it!
Problem
Given an array and chunk size, divide the array into many subarrays where each subarray is of length size.
I believe conceptualizing this problem is easier when we can see some of the expected outputs...
chunk([1, 2, 3, 4], 2) //→ [[1, 2], [3, 4]]
chunk([1, 2, 3, 4, 5], 2) //→ [[1, 2], [3, 4], [5]]
chunk([1, 2, 3, 4, 5, 6, 7, 8], 3) //→ [[1, 2, 3], [4, 5,6], [7, 8]]
chunk([1, 2, 3, 4, 5], 4) //→ [[1, 2, 3, 4], [5]]
chunk([1, 2, 3, 4, 5], 10) //→ [[1, 2, 3, 4, 5]]
Here we can see that the "size" number is for how many elements are in each subarray. As we can see they are not always even, so we want to make sure that the extra elements in our original array will plug into a final and smaller subarray.
Alrighty! I will throw in line by line explanations, but... Let’s code already ☺.
First Solution
This is the solution that is likely a bit more evident, especially to somebody who is not as practiced in JavaScript. Orrrr maybe your interviewer asks you to solve the problem without a few of the fancier JS methods. You never know!
function chunk(array, size) {
//declaring variable 'chunked' as an empty array
let chunked = [];
//for loop iterating through every element of our input array
for (let ele of array) {
//declaring variable 'last' as the last index of our 'chunked' array
const last = chunked[chunked.length-1];
//checking if last is undefined or if the last subarray is equal to the size
if (!last || last.length === size) {
//then we push the element to be a new subarray in 'chunked'
chunked.push([ele])
} else {
//if not, then we add the element to the 'last' subarray
last.push(ele)
}
}
//return the array of subarrays
return chunked
}
Second Solution
In this second solution (and probably the one that comes the most naturally to me) we make use of the .slice method. If you are unfamiliar, please reference the docs for .slice here! The key thing to remember here is that calling .slice will return a new array!
function chunk(array, size) {
//declaring variable 'chunked' as an empty array
let chunked = []
//setting our start point for our while loop at index 0
let i = 0;
//looping through the array until we have reached the final index
while (i < array.length) {
//push the sliced subarray of length 'size' into our new 'chunked' array
chunked.push(array.slice(i, i + size))
//increment by the size as to avoid duplicates
i += size;
}
//return the array of subarrays
return chunked
}
Third Solution
This last solution is probably the fanciest (and shortest). Personally, I have not gotten super familiar with the .splice method, but I know it can occasionally lend itself handy in problems like these. Again, please reference the docs here! Specifically for this solution's needs, I would scroll down a bit to reference how it is used when removing elements... but it can also do a few other things. The key thing to remember here is that, unlike .slice, the .splice method mutates the original array!
function chunk(array, size) {
//declaring variable 'chunked' as an empty array
let chunked = []
//looping through the array until it has been entirely "manipulated" or split into our subarrays
while(array.length > 0) {
//taking the spliced segments completely out of our original array
//pushing these subarrays into our new "chunked" array
chunked.push(array.splice(0, size))
}
//returning the new array of subarrays
return chunked
}