Split Array into Chunks
Split Array into Chunks is a reusable JavaScript snippet. Divides an array into smaller arrays (chunks) of a specified size. Useful for pagination and batch processing. The full implementation: function chunk(arr, size = 2) { const result = [] for (let i = 0; i < arr.length; i += size) { result.push(arr.slice(i, i + size)) } return result } // Generator version (memory efficient) function* chunkGen(arr, size = 2) { for (let i = 0; i < arr.length; i += size) { yield arr.slice(i, i + size) } } // Usage chunk([1, 2, 3, 4, 5], 2) // [[1, 2], [3, 4], [5]] // Process in batches async function… This snippet uses only standard JavaScript with no dependencies, so it runs in modern browsers and Node.js alike. Browse more patterns in the snippet library, or paste the code straight into the playground to experiment with it.