# Split an array into chunks

Build consecutive chunks without changing the original array.

Canonical: https://1loc.dev/array/split-an-array-into-chunks/
Version: 1.0.0
Runtime: JavaScript · Node.js 22 / modern browsers
Verification: Local fixtures; no external services
Updated: 2026-10-28

## Implementation

```javascript
function chunk({ values, size }) { return Array.from({ length: Math.ceil(values.length / size) }, (_, i) => values.slice(i * size, (i + 1) * size)); }
```

## Input

```json
{
  "values": [
    "ana@example.com",
    "rui@example.com",
    "lia@example.com",
    "eva@example.com",
    "ian@example.com",
    "noa@example.com",
    "teo@example.com"
  ],
  "size": 5
}
```

## Expected output

```json
[
  [
    "ana@example.com",
    "rui@example.com",
    "lia@example.com",
    "eva@example.com",
    "ian@example.com"
  ],
  [
    "noa@example.com",
    "teo@example.com"
  ]
]
```

## Edge case

Input:
```json
{
  "values": [],
  "size": 3
}
```
Output:
```json
[]
```

## Limits

- Size must be an integer from 1 to 1,000; zero is rejected before allocation.
- Chunks are shallow copies. The final chunk may be shorter.
- Example: Oveyon’s POST /v1/send accepts up to 5 recipients (to + cc + bcc) per message. Give each batch its own idempotency key; reusing one key returns the first batch’s message instead of sending the second.

The lab validates types and size before calling this function. Add equivalent validation at your own trust boundary.

## Source and license

Original: https://github.com/phuocng/1loc/blob/14b481cf60dff1620ab1bb99a43b1f68068cdd9b/contents/split-an-array-into-chunks.mdx
Author: Nguyen Huu Phuoc and original contributors
Contributors: See original repository history
Source commit: 14b481cf60dff1620ab1bb99a43b1f68068cdd9b
Copyright (c) 2020 phuoc-ng
License: MIT — https://1loc.dev/licenses/
Maintainer: Oveyon
Modifications: Curated implementation; explicit input bounds, edge cases, local fixtures and rebuilt presentation. Original MDX retained as inert source evidence.
