Created
October 8, 2021 06:58
-
-
Save roshanca/13f2868787297219bcbbba845b535c1e to your computer and use it in GitHub Desktop.
chunk
This file contains 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
export function chunk<T>(a: Array<T>, n: number): Array<Array<T>> { | |
if (n === 0) { | |
return [a]; | |
} | |
const chunks: Array<Array<T>> = []; | |
let i = 0; | |
while (i < a.length) { | |
chunks.push(a.slice(i, (i += n))); | |
} | |
return chunks; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment