Function chunk

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

    Type Parameters

    • T

      Type of the elements in the input array.

    Parameters

    • array: readonly T[]

      The array to split into chunks.

    • size: number

      The size of the chunks. This must be a positive integer.

    Returns T[][]

    An array of chunks of the given size.

    import { chunk } from "@std/collections/chunk";
    import { assertEquals } from "@std/assert";

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

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