Chunk
JS Functions
Easy
10 min
arrays
lodash
Implement chunk(array, size). It splits array into consecutive groups ("chunks") of size elements and returns them as a new array of arrays. If the elements don't divide evenly, the last chunk holds whatever is left over.
Chunking shows up whenever a list has to be processed in pieces: rendering a grid row by row, sending items to an API that accepts at most 50 per request, or paging through results.
chunk(['a', 'b', 'c', 'd'], 2); // [['a', 'b'], ['c', 'd']]
chunk([1, 2, 3, 4, 5], 2); // [[1, 2], [3, 4], [5]]
chunk([1, 2, 3], 5); // [[1, 2, 3]]
chunk([1, 2, 3]); // [[1], [2], [3]] (size defaults to 1)
chunk([], 3); // []
Requirements
sizedefaults to1.- A non-integer
sizeis rounded down (2.7behaves like2). - If
sizeis less than1after rounding, return an empty array. - Return a new array. The input array must not be modified.
- Elements are copied by reference (a shallow chunk), in their original order.
Follow-up
- How would you write
chunkfor an iterable or a stream that you can't index into? - How would you use
chunkto send a large list of IDs to an API in batches, with at most two requests in flight?
Spotted a mistake or a rights concern? Report an issue