Split an array

const splitInto = n => (accm, value) => accm.every(e => e.length === n) ?
  [...accm, [value]] :
  [...accm.slice(0, -1), [...accm[accm.length - 1], value]]

Split an array into chunks with a specified size.

ex.

[1,2,3,4,5,6,7,8].reduce(sliceBy(2), [])
// -> [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ], [ 7, 8 ] ]

[1,2,3,4,5,6,7,8].reduce(sliceBy(3), [])
// -> [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8 ] ]
BETA Snippet explanation automatically generated by OpenAI:

Here is what the above code is doing:
1. It checks if the array is of length n.
2. If it is, then it pushes the value to the end of the array.
3. If it is not, then it slices the array and pushes the value to the end of the sliced array.
\

Snippet By mktoho

ยท

Created February 5th, 2022

ยท

Report Snippet