Last active
July 10, 2020 17:15
-
-
Save dragonza/17911fd008b8e3e0719a63f75f4348e0 to your computer and use it in GitHub Desktop.
Chunk array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function chunk(array, size) { | |
const chunked_arr = []; | |
for (let i = 0; i < array.length; i++) { | |
const last = chunked_arr[chunked_arr.length - 1]; | |
if (!last || last.length === size) { | |
chunked_arr.push([array[i]]); | |
} else { | |
last.push(array[i]); | |
} | |
} | |
return chunked_arr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment