chunk

array

Creates an array of elements split into groups the length of size. If array can't be split evenly, the final chunk will be the remaining elements.

Signature

chunk<T>(array: T[], size: number): T[][]

Parameters

NameTypeDescription
arrayT[]The array to process
sizenumberThe length of each chunk

Returns

T[][] - Returns the new array of chunks

Examples

Basic Usage

import { chunk } from 'dashlite'

const result = chunk([1, 2, 3, 4, 5], 2)
console.log(result)

Output:

[[1, 2], [3, 4], [5]]

With Strings

import { chunk } from 'dashlite'

const result = chunk(['a', 'b', 'c', 'd'], 3)
console.log(result)

Output:

[["a", "b", "c"], ["d"]]

Available since version 1.0.0