Created
May 10, 2022 09:46
-
-
Save nitaking/75b850f390c88fbc625408ea5a043849 to your computer and use it in GitHub Desktop.
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
function chunk(arr, size) { | |
return arr.reduce( | |
(newarr, _, i) => (i % size ? newarr : [...newarr, arr.slice(i, i + size)]), | |
[] | |
) | |
} |
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
function chunk<T extends any[]>(arr: T, size: number) { | |
return arr.reduce( | |
(newarr, _, i) => (i % size ? newarr : [...newarr, arr.slice(i, i + size)]), | |
[] as T[][] | |
) | |
} |
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
// https://www.typescriptlang.org/play?#code/GYVwdgxgLglg9mABBAFuA1gHgCqIKYAeUeYAJgM6ICGYAngNoC6AfABRUBOHAXItgDSJyMAF55eYEAFsARng4BKRAG8AsAChEWxBzxQQHJJw4A6XaRAQ8rDdruJWYPAHdjggPqCYSgLzMHMIgApEKieIgA-IhOrlyIvPQmSTFu1Fwm5AA2MFasMF6IANShYgqMCvy29lpM1JTYTExVWgoaAL4aGgD0XYgAtP709ACMggBMggDMjIL0ACyCAKyCAGwziPQA7IyMGhAI5HCZeCakeDIgAOasqBisI+NTgguIy4grgtvPCq2aGkA | |
function chunk<T extends any[]>(arr: T, size: number) { | |
return arr.reduce( | |
(newarr, _, i) => (i % size ? newarr : [...newarr, arr.slice(i, i + size)]), | |
[] as T[][] | |
) | |
} | |
// -> [[1, 2, 3], [4, 5, 6], [7]] | |
console.debug(chunk([1, 2, 3, 4, 5, 6, 7], 3)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment