divideIntoChunkList
The divideIntoChunkList
function splits an array into smaller chunks of a specified size. It handles cases where the array length is not perfectly divisible by the chunk size and returns the remaining elements in the final chunk.
#
Function Signatureconst divideIntoChunkList = <E>(arr: E[], size: number) => E[][];
#
Parametersarr: E[]
: The array to be divided into chunks.
size
: number: The size of each chunk. Must be greater than 0.
#
ReturnsE[][]
: A two-dimensional array where each inner array is a chunk of the specified size.
#
Example Usage#
Example 1: Divide Array into Chunksconst arr = [1, 2, 3, 4, 5];const size = 2;const result = divideIntoChunkList(arr, size);console.log(result); // Output: [[1, 2], [3, 4], [5]]
#
Example 2: Handle Array Length Not Perfectly Divisible by Sizeconst arr = [1, 2, 3, 4, 5, 6, 7];const size = 3;const result = divideIntoChunkList(arr, size);console.log(result); // Output: [[1, 2, 3], [4, 5, 6], [7]]
#
Example 3: Handle Empty Arrayconst arr: any[] = [];const size = 3;const result = divideIntoChunkList(arr, size);console.log(result); // Output: []
#
Example 4: Handle Size Larger Than Array Lengthconst arr = [1, 2, 3];const size = 5;const result = divideIntoChunkList(arr, size);console.log(result); // Output: [[1, 2, 3]]
#
Example 5: Handle Size of 1const arr = [1, 2, 3, 4];const size = 1;const result = divideIntoChunkList(arr, size);console.log(result); // Output: [[1], [2], [3], [4]]
#
Example 6: Handle Size of 0 or Negativeconst arr = [1, 2, 3, 4];
try { divideIntoChunkList(arr, 0);} catch (e) { console.log(e.message); // Output: 'Invalid input: size should set 0 over'}
try { divideIntoChunkList(arr, -2);} catch (e) { console.log(e.message); // Output: 'Invalid input: size should set 0 over'}
#
ExplanationInput Validation
: The function checks if the size is less than or equal to 0. If so, it throws an error with a message: 'Invalid input: size should set 0 over'.Chunk Creation
: The function uses a while loop to slice the array into chunks of the specified size. It continues until all elements of the array have been processed.Return Value
: The function returns a two-dimensional array where each inner array represents a chunk of the original array.