• Category

    Divides an array into several chunks of size.

    Time complexity: O(n)

    Space complexity: O(n)

    Type Parameters

    • T

    Parameters

    • array: readonly T[]

      The array to chunk

    • size: number

      The size of each chunk

    Returns Table<T>

    The new array containing chunks of the original array

    Array

    Example

    chunk([1, 2, 3, 4, 5, 6], 2); // [[1, 2], [3, 4], [5, 6]]
    

    Example

    chunk([1, 2, 3, 4, 5, 6], 5); // [[1, 2, 3, 4, 5], [6]]
    

    Example

    chunk([1, 2, 3, 4, 5, 6], 6); // [[1], [2], [3], [4], [5], [6]]
    

    Example

    chunk([1, 2, 3, 4, 5, 6], 100); // [[1, 2, 3, 4, 5, 6]]
    
  • Category

    Divides an iterable into several chunks of size.

    Time complexity: O(n)

    Space complexity: O(n)

    Type Parameters

    • T

    Parameters

    • iterable: Iterable<T>

      The iterable to chunk

    • size: number

      The size of each chunk

    Returns IterableIterator<T[]>

    An iterator yieliding new arrays containing chunks of the original iterable

    Iterable

    Example

    [...chunk(new Set([1, 2, 3, 4, 5, 6]), 2)]; // [[1, 2], [3, 4], [5, 6]]
    

    Example

    [...chunk(new Set([1, 2, 3, 4, 5, 6]), 5)]; // [[1, 2, 3, 4, 5], [6]]
    

    Example

    [...chunk(new Set([1, 2, 3, 4, 5, 6]), 6)]; // [[1], [2], [3], [4], [5], [6]]
    

    Example

    [...chunk(new Set([1, 2, 3, 4, 5, 6]), 100)]; // [[1, 2, 3, 4, 5, 6]]