Skip to main content

chunk

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.

API

function chunk<T>(arr: T[], chunkSize: number = 1): T[][];

Usage

import { chunk } from "@feedzai/js-utilities";

chunk(['a', 'b', 'c', 'd', 'e', 'f', 'g'])
// => [['a'], ['b'], ['c'], ['d'], ['e'], ['f'], ['g']]

chunk(['a', 'b', 'c', 'd', 'e', 'f', 'g'], 3)
// => [['a', 'b', 'c'], ['d', 'e', 'f'], ['g']]

chunk(['a', 'b', 'c', 'd', 'e', 'f', 'g'], 0)
// => []

chunk(['a', 'b', 'c', 'd', 'e', 'f', 'g'], -1)
// => []