Function chunk

  • Namespace

    ArrayUtils

    Split an array into smaller arrays of a specified size.

    Type Parameters

    • T

      The type of elements in the array.

    Parameters

    • arr: T[]

      The array to be chunked.

    • size: number

      The size of each chunk.

    Returns T[][]

    An array of smaller arrays (chunks).

    Example

    const originalArray = [1, 2, 3, 4, 5, 6, 7, 8, 9];
    const chunkedArray = chunkArray(originalArray, 3);
    console.log(chunkedArray);
    // Output: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

    Example

    const names = ["Alice", "Bob", "Charlie", "David", "Eve"];
    const nameChunks = chunkArray(names, 2);
    console.log(nameChunks);
    // Output: [["Alice", "Bob"], ["Charlie", "David"], ["Eve"]]

Generated using TypeDoc