How to find the Total Number of Elements in an Array in TypeScript ?
Last Updated :
06 May, 2024
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:
Using the length property
The length property is used to get the length or size of an array. It can be used to get the total number of elements stored in the array.
Syntax:
array.length
Example: The below code implements length property to find total number of elements in an array.
JavaScript
const array: number[] =
[1, 2, 3, 4, 5];
const lengthUsingLengthProperty: number =
array.length;
console.log("Total number of elements: ",
lengthUsingLengthProperty);
Output:
Total number of elements: 5
Using the forEach method
The forEach method iterates over each element of the array thus incrementing a counter for each element and counting the number of elements in the array
Syntax:
array.forEach(() => {count++;} );
Example: The below code will explain the use of the forEach method to find number of elements in an array.
JavaScript
const array: number[] = [1, 2, 3, 4, 5];
let count: number = 0;
array.forEach(() => {
count++;
});
console.log("Total number of elements: ",
count);
Output:
Total number of elements: 5
Using the reduce method
The reduce
method accumulates the count of elements in the array by reducing the element in the array until array becomes empty.
Syntax:
const count = array.reduce((acc) => acc + 1, 0);
Example: The below code will explain the use of the reduce method to find total number of elements in an array.
JavaScript
const array: number[] =
[1, 2, 3, 4, 5];
const count: number =
array.reduce(
(acc) => acc + 1, 0);
console.log("Total number of elements:",
count);
Output:
Total number of elements: 5
Using a for Loop
In this approach we Iterating through the array using a for loop, incrementing a counter for each element until the end of the array is reached, then returning the total count.
Example: In this example The function iterates through the array using a `for` loop, incrementing a count for each element, then returns the count. Finally, it logs the total count.
JavaScript
function getTotalElements(arr: any[]): number {
let count = 0;
for (let i = 0; i < arr.length; i++) {
count++;
}
return count;
}
const array = [1, 2, 3, 4, 5];
const totalElements = getTotalElements(array);
console.log("Total number of elements:", totalElements);
Output:
Total number of elements: 5
Similar Reads
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 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
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 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 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 Create Arrays of Generic Interfaces in TypeScript ? In TypeScript, managing data structures effectively is crucial for building robust applications. Arrays of generic interfaces provide a powerful mechanism to handle varied data types while maintaining type safety and flexibility. There are various methods for constructing arrays of generic interface
3 min read