Recently I was working with some data coming out of a Redis pipeline and I
wanted to take the data and split it up into an array of X items per key. I was
working with PHP, this wouldn’t have been a big deal, just use
get on with my day.
There are definitely options though. In fact, there are 46 packages out on npm at the time of this writing that appear to handle splitting / chunking an array.
While these options are completely viable, it seemed like overkill to me to introduce a new dependency just to handle something as trivial as creating an array from an array one time in my codebase.
The thing is, the concept of chunking isn’t anything complex. Start with an array and transform it into another array. Could handle it a handful of different ways, as well.
Here’s the quick and dirty that I went with to get the job done. For the sake of the blog post, the first line is generating an array with 100 numerical items:
But I mean, if you would rather add an additional dependency to your codebase, by all means, don’t let me stop you ;)