Algorithms and Data Structures Part V
Array Chunking
First off, if you haven’t read my other parts on Algorithms click here to get started.
Okay, what is array chunking? Array chunking is a technique used to split very large arrays into smaller groups, usually for easier manipulation and presentation. With that understanding let’s get started.
Array Chunking
We are given an array and chunk size, asked to divide the array into many subarrays where each subarray is of length, size.
e.g. chunk([1,2,3,4], 2) → [[1,2], [3,4]]
the initial array is split into two subarrays within the chunk.
e.g. chunk([1,2,3,4,5], 2 → [[1,2], [3,4], [5]]
the initial array is split into 3 subarrays because we specified a chunk length of 2, but with one number left over we just push that into another chunk.
So there are two ways of doing this and I will go over both.
1st:
We start off with our function taking in array and size as the arguments.
Now, step one we need to create a new array that’s going to hold all the different chunks.
Now we need to iterate through the original array of elements. To do this we will use the for of helper.
Then we will execute some logic inside of here to look at the last element inside our chunked array. To do this we will make a temperary variable…
We say chunked at chunked.length -1. This then gets us the last element inside the array.
Now we need to check two things. Does the last element exist? Is it’s length equal to the chunk size? If it is then push a new chunk into chunked with the current element we are iterating over. To do this we will say…
if last does not exist or if the length of last is equal to the chunk size then add a new element to chunked and put that element inside there. Notice that we put our chunk [] and then element inside that chunk[] at the same time.
Last on this snapshot we see we are handling our else statement. This is saying if we already have a chunk but it is not yet full then, take the current element and add it to the chunk which is the last variable.
Finally NEVER forget to return the chunked array at the bottom below is the full algorithm.
And that’s it!!
Real quick let’s look at another way of handling this problem…
This is using the slice function on the array. As you may see, we start with our empty chunked array and we then have an index variable starting off at 0. We use let instead of const here because out index variable will be changing over time. Next we have our while loop. We want to run this as long as index is less than the original arrays length.
while (index < array.length) {}
In here we slice everything from index to index + size
array.slice(index, index + size)
and put it directly into the chunked array using the push method.
chunked.push(array.slice(index, index + size))
finally we take our index variable and add size to it so we increment by the size variable.
index += size
And finally return chunked at the bottom. Heres the full code again…
I hope this helped, either solution works great, the second may be harder to come up with on your own than the first but you can choose either way for yourself.
As always thanks for reading and see you next week for part VI.