@bnidev/js-utils
    Preparing search index...

    Function chunkArray

    • Splits an array into chunks of a specified size.

      Type Parameters

      • T

      Parameters

      • arr: T[]

        The array to be chunked.

      • size: number

        The size of each chunk.

      Returns T[][]

      An array of arrays, where each sub-array is a chunk of the specified size.

      This utility is useful for processing large arrays in smaller, manageable pieces, such as when handling pagination or batching operations. It can help improve performance and reduce memory usage by avoiding operations on very large arrays at once.

      // ES Module
      import { chunkArray } from '@bnidev/js-utils'

      // CommonJS
      const { chunkArray } = require('@bnidev/js-utils')
      // Chunk an array into sub-arrays of size 2
      const numbers = [1, 2, 3, 4, 5, 6]
      const chunked = chunkArray(numbers, 2)
      // chunked will be [[1, 2], [3, 4], [5, 6]]