Created
April 13, 2020 02:20
-
-
Save andreigec/677b1b7ddc66c380bdc8c3343a5e26c8 to your computer and use it in GitHub Desktop.
chunk-array.ts
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>(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