Skip to content

Instantly share code, notes, and snippets.

@andreigec
Created April 13, 2020 02:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreigec/677b1b7ddc66c380bdc8c3343a5e26c8 to your computer and use it in GitHub Desktop.
Save andreigec/677b1b7ddc66c380bdc8c3343a5e26c8 to your computer and use it in GitHub Desktop.
chunk-array.ts
export function chunk<T>(array: T[], size: number): T[][] {
const chunkedArr = [];
const copied = [...array]; // ES6 destructuring
const numOfChild = Math.ceil(copied.length / size); // Round up to the nearest integer
for (let i = 0; i < numOfChild; i++) {
chunkedArr.push(copied.splice(0, size));
}
return chunkedArr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment