chunk

Split an array into smaller arrays of a fixed size, returning a new array of chunks without mutating the original input array.

Usage

chunk splits an array into multiple sub-arrays of a given size. Commonly used for pagination, batch processing, and data partitioning.

import { chunk } from '@movk/core'

const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
const chunks = chunk(numbers, 3)
console.log(chunks) // [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

const names = ['Alice', 'Bob', 'Charlie', 'David', 'Eve']
const pairs = chunk(names, 2)
console.log(pairs) // [['Alice', 'Bob'], ['Charlie', 'David'], ['Eve']]

API

chunk<T>(arr, size)

Split an array into chunks of the specified size.

Parameters

arr
T[] required
The array to split.
size
number required
The size of each chunk.

Returns

returns
T[][]
A two-dimensional array where each sub-array has at most size elements.

Changelog

No recent changes
Copyright © 2024 - 2026 YiXuan - MIT License