mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-14 01:49:41 +01:00
25 lines
450 B
JavaScript
25 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;
|
||
|
};
|