Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Transform an array to some chunk array.
function chunk(arr, chunk) {
var res = [],
len = arr.length,
_len = Math.ceil(arr.length / chunk),
_chunk = 0;
chunk = chunk > len ? len : chunk;
for (var i = 0; i < _len ;i++) {
var _arr = [];
if (i === _len - 1 && chunk > _len) {
for (var j = 0; j < (len - chunk); j++) {
_arr.push(arr[_chunk + j]);
}
} else {
for (var j = 0; j < chunk; j++) {
_arr.push(arr[_chunk + j]);
}
}
_chunk = chunk + _chunk;
res.push(_arr);
}
return res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.