std/collections/chunk.ts

Deno standard library
Go to Latest
function chunk
import { chunk } from "https://deno.land/std@0.139.0/collections/chunk.ts?s=chunk";

Splits the given array into chunks of the given size and returns them

Example:

import { chunk } from "https://deno.land/std@0.139.0/collections/mod.ts";
import { assertEquals } from "https://deno.land/std@0.139.0/testing/asserts.ts";

const words = [ 'lorem', 'ipsum', 'dolor', 'sit', 'amet', 'consetetur', 'sadipscing' ]
const chunks = chunk(words, 3)

assertEquals(chunks, [
    [ 'lorem', 'ipsum', 'dolor' ],
    [ 'sit', 'amet', 'consetetur' ],
    [ 'sadipscing' ],
])

Parameters

array: readonly T[][src]
size: number[src]