Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Chunks array into equally large sizes as far as it's possible
- *
- * Example chunkArray([1,2,3,4,5,6,7,8,9,10,11], 3)
- * Returns [[1,2,3,4],[5,6,7,8],[9,10,11]]
- */
- function chunkArray(array, size) {
- if (size < 2) return array
- let len = array.length
- let chunks = []
- let i = 0
- let n
- if (len % size === 0) {
- n = Math.floor(len / size)
- while (i < len) {
- chunks.push(array.slice(i, i += n))
- }
- } else {
- while (i < len) {
- n = Math.ceil((len - i) / size--)
- chunks.push(array.slice(i, i += n))
- }
- }
- return chunks
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement