Creates an array of elements split into groups the length of size
.
/**
* @param {Array} array The array to process.
* @param {number} [size=1] The length of each chunk
* @returns {Array} Returns the new array of chunks.
*/
function chunk(array, size) {
size = Math.max(size, 0);
const length = array == null ? 0 : array.length;
if (!length || size < 1) {
return [];
}
let index = 0;
let resIndex = 0;
const result = new Array(Math.ceil(length / size));
while (index < length) {
result[resIndex++] = slice(array, index, (index += size));
}
return result;
}