Skip to content

Instantly share code, notes, and snippets.

@akwodkiewicz
Last active December 20, 2021 12:15
Show Gist options
  • Save akwodkiewicz/f6d26ab520000a28b999669852ff3794 to your computer and use it in GitHub Desktop.
Save akwodkiewicz/f6d26ab520000a28b999669852ff3794 to your computer and use it in GitHub Desktop.
Handy TS/JS functions
// Group elements in `xyz` array in `groupSize`-length arrays
const xyz = ['a','b','c','d','e','f','g','h','i'];
const groupSize = 3;
xyz.reduce<string[][]>((result, item, idx) =>
new Boolean((idx % groupSize
? result[result.length-1].push(item)
: result.push([item])))
&& result,
[]);
// > [ [ 'a', 'b', 'c' ], [ 'd', 'e', 'f' ], [ 'g', 'h', 'i' ] ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment