@nevware21/ts-utils
    Preparing search index...

    Function arrChunk

    • Function

      The arrChunk() method returns a new array with elements divided into groups of a specified size. The last group may have fewer elements if the array length is not divisible by the chunk size.

      Type Parameters

      • T

        Identifies the base type of array elements

      Parameters

      • theArray: ArrayLike<T>

        The array or array-like object to chunk

      • size: number

        The size of each chunk. Must be a positive integer

      Returns T[][]

      A new array of chunks, where each chunk is an array of the specified size

      0.14.0

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

      // Array-like objects
      arrChunk({ length: 5, 0: "a", 1: "b", 2: "c", 3: "d", 4: "e" }, 2);
      // [["a", "b"], ["c", "d"], ["e"]]