Created
June 21, 2019 14:38
-
-
Save barnslig/18c759b8970ba04c66f506e68b94c2d2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* Turn an array into chunks | |
* | |
* @param {Array} arr - Array that should be chunked | |
* @param {number} size - Chunk size | |
* @returns {Array} Array of Arrays where each array has at max `size` elements | |
*/ | |
const chunkify = (arr, size) => | |
arr.reduce( | |
(acc, cur) => | |
acc.slice(-1)[0].length < size | |
? [...acc.slice(0, -1), acc.slice(-1)[0].concat([cur])] | |
: [...acc, [cur]], | |
[[]] | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment