#Arrays

Split an Array Into Chunks

Split an array into groups of a fixed size, with whatever is left over in a shorter final group.

By Pratik RaiEasy

Write chunk(arr, size) splitting the array into subarrays of length size.

The final group holds the remainder and may be shorter. A size of zero or less cannot produce groups — return an empty array.

Examples

Input:

JSfile.javascript
1chunk(['a', 'b', 'c', 'd', 'e', 'f', 'g'], 3);

Output:

[['a', 'b', 'c'], ['d', 'e', 'f'], ['g']]

Seven items in groups of three: two full groups, then one item left over.

Constraints

  • arr may be empty, and size may be larger than its length
  • size is an integer, and zero or a negative size returns []

Notes

  • slice past the end of an array simply stops, so the short final group needs no special case.

How to solve it

Stepping by size

A loop that steps by size instead of by one, with slice doing the extraction.

Approach

The short final group needs no special case, because slice past the end simply stops at the end.

Implementation

JSfile.javascript
1function chunk(arr, size) { 2 if (size <= 0) return []; 3 const out = []; 4 for (let i = 0; i < arr.length; i += size) out.push(arr.slice(i, i + size)); 5 return out; 6}

Worth knowing

The input worth thinking about is a size of zero or less. i += 0 never advances, so an unguarded loop hangs forever — the kind of thing that takes down a tab rather than throwing. Decide what it returns and guard before the loop.

slice copies, so the groups are independent of the input array.

Goal: Return groups of the requested size, with a short final group when needed.

Frequently asked questions

How do you split an array into chunks?
Loop with a step equal to the chunk size and take a slice each iteration. The final slice returns whatever is left, so a short last group needs no special handling.
What should happen with a size of zero?
Nothing sensible can be produced, and an unguarded loop with i += 0 never advances — it hangs the tab rather than throwing. Guard it and return an empty array.
Does chunking copy the data?
slice produces new arrays holding the same element references, so the groups are independent containers but the objects inside them are still shared with the input.
Where is chunking useful?
Paginating a list in memory, laying out a grid row by row, or splitting a large upload or API payload into batches the server will accept.

Related Challenges

Continue learning with these related challenges

View All
JavaScript

Array.prototype.map Polyfill

Implement Array.prototype.map from scratch. Master callback signatures, thisArg binding, and sparse-array hole preservation.

JavaScript · Arrays · Polyfills — Pratik Rai ·

JavaScript

Array.prototype.filter Polyfill

Implement Array.prototype.filter from scratch. The key detail: push the original element, not the boolean result of the predicate.

JavaScript · Arrays · Polyfills — Pratik Rai ·

JavaScript

Array.prototype.flat Polyfill

Implement Array.prototype.flat from scratch with a configurable depth. Default depth is 1, not Infinity.

JavaScript · Arrays · Recursion — Pratik Rai ·