Skip to content

Instantly share code, notes, and snippets.

@nojaf
Created July 12, 2017 12:55
Show Gist options
  • Save nojaf/f92824b93c35a11d67ef3f4fce48e1ba to your computer and use it in GitHub Desktop.
Save nojaf/f92824b93c35a11d67ef3f4fce48e1ba to your computer and use it in GitHub Desktop.
Chunk & flatten
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