Skip to content

Instantly share code, notes, and snippets.

@roshanca
Created October 8, 2021 06:58
Show Gist options
  • Save roshanca/13f2868787297219bcbbba845b535c1e to your computer and use it in GitHub Desktop.
Save roshanca/13f2868787297219bcbbba845b535c1e to your computer and use it in GitHub Desktop.
chunk
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