How to Split an Array of Strings into Chunks of Equal Length in TypeScript ?
Last Updated :
28 Apr, 2025
In TypeScript, to split an array of strings into chunks of equal length we need to divide the array into the smaller subarrays, in which each contains the number of elements based on the chunk size.
These are the following approaches:
Using for Loop
In this approach, we use the for loop to iterate through the input array, by incrementing the index which is specified by the chunk size, and push the subarrays of the input array into the result array.
Syntax:
for (initialization; condition; increment/decrement) {
// code
}
Example: The below example uses for loop to split an array of strings into chunks of equal length in TypeScript.
JavaScript
const approach1Fn = (arr: string[], size: number):
string[][] => {
const res: string[][] = [];
for (let i = 0; i < arr.length; i += size) {
res.push(arr.slice(i, i + size));
}
return res;
};
const arr = ["Geeks", "for", "Geeks", "is",
"a", "computer", "science", "portal"];
const size = 2;
const result = approach1Fn(arr, size);
console.log(result);
Output:
[["Geeks", "for"], ["Geeks", "is"], ["a", "computer"], ["science", "portal"]]
Using Array.from() method
In this approach, we use Array.from() method to create the new array with the size which is determined by dividing the original array's length with the size specified as the chunk size. Then the mapping function slices the input into chunks based on the calculated indexes.
Syntax:
Array.from(iterable, mapFunction?)
Example: The below example uses the Array.from() method to split an array of strings into chunks of equal length in TypeScript.
JavaScript
const approach2Fn = (arr: string[], size: number):
string[][] =>
Array.from({ length: Math.ceil(arr.length / size) },
(_, index) =>
arr.slice(index * size, (index + 1) * size)
);
const arr = ["Geeks", "for", "Geeks", "is",
"a", "computer", "science", "portal"];
const size = 2;
const result = approach2Fn(arr, size);
console.log(result);
Output:
[["Geeks", "for"], ["Geeks", "is"], ["a", "computer"], ["science", "portal"]]
Using reduce() method
In this approach, we use the reduce() method to iterate over the input array. The accumulator is updated which is based on the current index and if the index is a multiple of the specified chunk size, then a new subarray is appended to the result by slicing the input array.
Syntax:
array.reduce(callback, initialValue?)
Example: The below example uses the reduce() method to split an array of strings into chunks of equal length in TypeScript.
JavaScript
const approach3Fn = (arr: string[], size: number):
string[][] =>
arr.reduce((result: string[][], _, index) =>
(index % size === 0 ? [...result,
arr.slice(index, index + size)] : result),
[]);
const arr = ["Geeks", "for", "Geeks",
"is", "a", "computer", "science", "portal"];
const size = 2;
const result = approach3Fn(arr, size);
console.log(result);
Output:
[["Geeks", "for"], ["Geeks", "is"], ["a", "computer"], ["science", "portal"]]
Similar Reads
How to Declare an Array of Strings in TypeScript ? Arrays are fundamental data structures in TypeScript, enabling developers to manage collections of elements efficiently. Below are the approaches to declare an Array of strings in TypeScript:Table of ContentSquare Brackets NotationArray ConstructorSquare Brackets NotationUsing square brackets notati
1 min read
How to Iterate Over Characters of a String in TypeScript ? In Typescript, iterating over the characters of the string can be done using various approaches. We can use loops, split() method approach to iterate over the characters. Below are the possible approaches: Table of Content Using for LoopUsing forEach() methodUsing split() methodUsing for...of LoopUs
2 min read
How to Convert an Array of Objects into Object in TypeScript ? Converting an array of objects into a single object is a common task in JavaScript and TypeScript programming, especially when you want to restructure data for easier access. In this article, we will see how to convert an array of objects into objects in TypeScript.We are given an array of objects a
3 min read
How to split each element of an array of strings into different categories using Node.js? The task is to split each element of a string array into different categories. Example: Input Array : const input = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqr', 'stw', 'xyz'] Category Array : const categories = ['a', 'b', 'c'] Split Size : const split_size = 3 Output:[ a: [ 'abc', 'def', 'ghi' ], b: [
2 min read
How to find the Total Number of Elements in an Array in TypeScript ? In TypeScript, arrays are a common data structure used to store collections of elements. You can store multiple elements of different or the same data type inside them by explicitly typing. The below methods can be used to accomplish this task: Table of Content Using the length property Using the fo
3 min read
How to Check Whether an Array Contains a String in TypeScript ? To check whether an array contains a string in typescript we have a different approach. In this article, we are going to learn how to check whether an array contains a string in typescript.Below are the approaches to check whether an array contains a string in typescript:Table of ContentApproach 1:
4 min read
How to get the Index of an Array based on a Property Value in TypeScript ? Getting the index of an array based on a property value involves finding the position of an object within the array where a specific property matches a given value. The below approaches can be used to accomplish the task. Table of Content Using Array.findIndex()Using reduce() methodUsing for loopUsi
4 min read
How to Declare a Fixed Length Array in TypeScript ? To declare a Fixed-length Array in TypeScript you can use a Tuple. Tuple types allow you to specify the types for each element in the array and, importantly, define a fixed number of elements in a specific order. In this article, we are going to learn how to declare a fixed-length array in TypeScrip
3 min read
How to Convert a Set to an Array in TypeScript ? A Set in TypeScript is used to create a particular type of list that does not contain duplicate elements. If any element is repeated more than once it will automatically remove the duplicate existence and consider it only once in the list. In this article, we will convert these types of lists into a
5 min read
How can I Define an Array of Objects in TypeScript? In TypeScript, the way of defining the arrays of objects is different from JavaScript. Because we need to explicitly type the array at the time of declaration that it will be an Array of objects. In this article, we will discuss the different methods for declaring an array of objects in TypeScript.
6 min read