DEV Community

Discussion on: Array Chunking

Collapse
 
jreina profile image
Johnny Reina

This is generally how I do it if I need to partition and I don't have a utility library.

const splitEvery = (arr, n) => arr.reduce((memo, val) => {
  if(memo[memo.length - 1].length === n) memo.push([]);
  memo[memo.length - 1].push(val);
  return memo;
}, [[]]);