Skip to content

Instantly share code, notes, and snippets.

@Woodsphreaker
Created October 21, 2017 00:57
Show Gist options
  • Save Woodsphreaker/6e4789636b0c0347eecbe0c965a539bd to your computer and use it in GitHub Desktop.
Save Woodsphreaker/6e4789636b0c0347eecbe0c965a539bd to your computer and use it in GitHub Desktop.
Chunk Array
const arr = [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]
const division = ( a, b ) => Math.ceil( a / b )
const sliceArray = size => acc => {
acc[1].push( acc[0].splice( 0, size ) );
return acc
};
const chunk = ( arr, size = 2 ) =>
Array( division( arr.length, size ) )
.fill()
.reduce( sliceArray( size ), [[].concat(arr), []])[1]
Array.from({ length: 10 }, ( _, i ) => chunk( arr, i + 1 ) )
chunk([...Array(200).keys()], 10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment