Back to library
📜JavaScriptjavascriptbeginner

Array Chunk

Split an array into chunks of specified size

arraysutilities

Code

1function chunk(array, size) {
2  const result = [];
3  
4  for (let i = 0; i < array.length; i += size) {
5    result.push(array.slice(i, i + size));
6  }
7  
8  return result;
9}
10
11// Usage example
12const numbers = [1, 2, 3, 4, 5, 6, 7, 8];
13console.log(chunk(numbers, 3));
14// Output: [[1, 2, 3], [4, 5, 6], [7, 8]]

Quick Tips

  • Click the "Copy" button to copy the code to your clipboard
  • This code is production-ready and can be used in your projects
  • Check out related snippets below for more examples

Related Snippets

Build Your Own Snippet Library

Organize your team's code snippets with Snippetly. Share knowledge and boost productivity across your organization.