mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-10 00:09:06 +01:00
24 lines
450 B
JavaScript
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;
|
|
};
|