Created
October 21, 2017 00:57
-
-
Save Woodsphreaker/6e4789636b0c0347eecbe0c965a539bd to your computer and use it in GitHub Desktop.
Chunk Array
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
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