Skip to content

chunk

The chunk function splits an array or string into smaller parts of fixed size.

Syntax

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

Parameters

NameTypeDescription
arrayT[] | stringSource array or string
sizenumberSize of each chunk

Returns

TypeDescription
T[][] | stringArray of chunks or divided string

Examples

typescript
chunk([1, 2, 3, 4, 5], 2); // => [[1,2],[3,4],[5]]
chunk("abcdef", 3); // => ["abc", "def"]

Notes

  • If the size is not positive, throws an error.
  • Useful for splitting data into smaller blocks.

References

Released under the MIT License.