chunk
The chunk()
function splits an array into chunks of a specified size.
Parameters
Parameter | Type | Description |
---|---|---|
arr | Array | The array to chunk. |
size | number | The size of each chunk. |
Returns
Array
: An array of chunks.
Throws
TypeError
: Ifarr
is not an array.
Example
const utils = require('utils-core.js');
const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const chunkedArray = utils.array.chunk(array, 3);
console.log(chunkedArray); // [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]