Skip to content

Instantly share code, notes, and snippets.

@barnslig
Created June 21, 2019 14:38
Show Gist options
  • Save barnslig/18c759b8970ba04c66f506e68b94c2d2 to your computer and use it in GitHub Desktop.
Save barnslig/18c759b8970ba04c66f506e68b94c2d2 to your computer and use it in GitHub Desktop.
/**
* 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