LeetCode/js/chunk-array.js
Matej Focko 2351dfd0ee
chore: unwrap one layer
Signed-off-by: Matej Focko <mfocko@redhat.com>
2023-12-12 14:36:00 +01:00

24 lines
450 B
JavaScript

/**
* @param {Array} arr
* @param {number} size
* @return {Array[]}
*/
var chunk = function(arr, size) {
let chunked = [];
let current = new Array();
for (let i = 0; i < arr.length; ++i) {
current.push(arr[i]);
if ((i + 1) % size == 0) {
chunked.push(current);
current = new Array();
}
}
if (current.length > 0) {
chunked.push(current);
}
return chunked;
};