es-next-tools
Version:
A comprehensive utility library for JavaScript and TypeScript that provides a wide range of functions for common programming tasks, including mathematical operations, date manipulations, array and object handling, string utilities, and more.
19 lines (18 loc) • 596 B
JavaScript
/**
* Divides an array into smaller chunks of a specified size.
*
* @param {T[]} array - The array to be divided into chunks.
* @param {number} size - The size of each chunk.
* @returns {T[][]} An array of arrays, where each inner array is a chunk of the original array.
*
* @example
* chunk([1, 2, 3, 4, 5], 2) // [[1, 2], [3, 4], [5]]
* chunk(['a', 'b', 'c', 'd'], 3) // [['a', 'b', 'c'], ['d']]
*/
export function chunk(array, size) {
const result = [];
for (let i = 0; i < array.length; i += size) {
result.push(array.slice(i, i + size));
}
return result;
}