To split an array of generics into chunks of size n, we can create a generic function that takes in an array of any type and a number for chunk size, then returns an array of arrays with each inner array containing the specified number of elements.
Here's the code for the function:
index.ts211 chars10 lines
This function uses a loop to iterate over the input array and slice it into smaller arrays of size chunkSize. The results are pushed into a new array, which is returned at the end.
To use this function, simply pass in your array and the desired chunk size:
index.ts164 chars4 lines
This will output an array of arrays, with each inner array containing three elements.
gistlibby LogSnag