— TypeScript, Array, Chunking — 2 min read
Chunking an array in TypeScript is a process of dividing an array into smaller arrays (also known as "subarrays"). This can be useful for a variety of purposes, such as pagination or dividing data into manageable chunks for processing.
There are a few different approaches you can take to chunk an array in TypeScript. One approach is to use the Array.prototype.slice()
method, which returns a new array that contains a portion of the original array. Here's an example of how you could use slice() to chunk an array:
1function chunkArray(array: any[], chunkSize: number): any[][] {2 const chunkedArray: any[][] = [];3 let index = 0;4
5 while (index < array.length) {6 chunkedArray.push(array.slice(index, index + chunkSize));7 index += chunkSize;8 }9
10 return chunkedArray;11}12
13const myArray = [1, 2, 3, 4, 5, 6, 7, 8, 9];14const chunkedArray = chunkArray(myArray, 3);15
16console.log(chunkedArray); // [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
In this example, we define a function chunkArray() that takes an array and a chunk size as arguments. We create an empty chunkedArray variable to store the resulting chunks. We then use a while loop to iterate over the original array, using slice()
to create a new chunk for each iteration. The index variable is used to keep track of the current position in the array, and is incremented by the chunk size at the end of each iteration.
Another approach to chunking an array in TypeScript is to use the Array.prototype.splice()
method, which removes elements from an array and returns them in a new array. Here's an example of how you could use splice()
to chunk an array:
1function chunkArray(array: any[], chunkSize: number): any[][] {2 const chunkedArray: any[][] = [];3
4 while (array.length > 0) {5 chunkedArray.push(array.splice(0, chunkSize));6 }7
8 return chunkedArray;9}10
11const myArray = [1, 2, 3, 4, 5, 6, 7, 8, 9];12const chunkedArray = chunkArray(myArray, 3);13
14console.log(chunkedArray); // [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
In this example, we again define a function chunkArray() that takes an array and a chunk size as arguments. We create an empty chunkedArray variable to store the resulting chunks. We then use a while loop to iterate over the original array, using splice()
to remove elements from the beginning of the array and push them into a new chunk. The loop continues until the array is empty.
There are a few things to keep in mind when using these approaches to chunk an array in TypeScript. First, both slice()
and splice()
operate on the original array, so if you want to preserve the original array, you will need to make a copy of it before chunking.
Second, both slice()
and splice()
have their own performance trade-offs, depending on how you use them. slice()
is generally faster for creating new arrays, but it does not modify the original array. splice()
, on the other hand, modifies the original array and can be slower as a result.
Finally, it's important to keep in mind that both slice()
and splice()
are array methods, which means they can only be used on arrays. If you need to chunk an array-like object (such as a NodeList or arguments object), you will need to convert it to an array first using the Array.from()
method or the spread operator (...).