Split array into chunks
Here's a succinct solution to your problem: the function creates a new array and uses a loop to extract sub-arrays of the given size from the input array with the slice method:
This one-liner offers a neat solution using Array.from and math to determine the number of chunks.
Advanced strategies for array chunking
Limitations of the brute-force method
The fast answer is a kind of brute-force method. It's pure fire and forget. But it may be inefficient for large arrays or when your chunk size is small.
Exploit ES6 features: reduce and concat
The ES6 reduce method combined with concat provides an elegant functional programming approach:
Generators: The lazy way is the efficient way
For large arrays, a generator function is more suitable as it yields chunks without creating an bulky intermediate array. Because less is more!
Enter TypeScript stage right
For bonus points and a crisp high-five from your compiler, use TypeScript for reliable type safety:
Caveats and guidelines for usage
Zero-size chunks: Cue endless loop
Ensure that chunkSize is not zero unless you've stocked up for the winter and are ready for an infinite coding session:
No uncontrolled magic
Your chunking function should behave predictably, a la Principle of Least Surprise. Remember, a magician never reveals their secrets, but your code should.
Keep it local
Confine chunk functions within local modules to avoid an outbreak of naming conflicts in the global namespace. Keep the global namespace clean and green!
Array chunking and performance
All chunking methods described have the same running time, however, not all are created equal when it comes to constant factors. Those avoiding intermediate structures are secretly doing pilates and hence can boast of better constant factor efficiency.
Additional areas to explore
Diverse uses of map and fill
The Array.map and Array.fill offer another dynamic duo for an expressive solution:
Iteration methods: The art of going through your array
Simple iteration methods such as forEach and for...of can also craft chunks, but they lack the superhero cape that Array.from and reduce wear.
DIY chunk method
You might be itching to add a custom method to the Array prototype. Simmer down, cowboy. This reeks of potential issues, especially in larger code projects.
Edge cases: Come to the edge, they said
Edge cases are like unexpectedly stepping on a Lego brick. Make sure your function can handle situations where the array cannot be evenly split.
Was this article helpful?