Created
July 12, 2017 12:55
-
-
Save nojaf/f92824b93c35a11d67ef3f4fce48e1ba to your computer and use it in GitHub Desktop.
Chunk & flatten
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
export function flatten<T>(array: T[][]): T[] { | |
return [].concat.apply([], array); | |
} | |
export function chunk<T>( | |
array: T[], | |
chunkSize: number, | |
acc: T[][] = [] | |
): T[][] { | |
return array.length > chunkSize | |
? chunk(array.slice(chunkSize), chunkSize, [ | |
...acc, | |
array.slice(0, chunkSize) | |
]) | |
: [...acc, array]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment