How to Iterate Array of Objects in TypeScript ?
Last Updated :
01 Aug, 2024
In TypeScript, we can iterate over the array of objects using various inbuilt loops and higher-order functions, We can use for...of Loop, forEach method, and map method.
There are several approaches in TypeScript to iterate over the array of objects which are as follows:
Using for...of Loop
In this approach, we use a for...of loop to iterate over an array of objects (arr), where each data represents an object of the defined interface obj. It prints the name and role properties of each object to the console.
Syntax:
for (const element of iterable) {
// code
}
Example: The below example uses for...of Loop to iterate an array of objects in TypeScript.
JavaScript
interface obj {
name: string;
role: string;
}
let arr: obj[] = [
{
name: 'GFG User 1',
role: 'Developer'
},
{
name: 'GFG User 2',
role: 'Designer'
},
{
name: 'GFG User 3',
role: 'Tester'
}
];
for (const data of arr) {
console.log(
`Name: ${data.name},
Role: ${data.role}`
);
}
Output:
Name: GFG User 1, Role: Developer
Name: GFG User 2, Role: Designer
Name: GFG User 3, Role: Tester
Using forEach method
In this approach, we are using the forEach method to iterate through an array of objects (arr), where each data object of type obj is accessed to print the name and role properties,
Syntax:
array.forEach((element: ElementType, index: number, array: ElementType[]) => {
// code
});
Example: The below example uses forEach method to iterate an array of objects in TypeScript.
JavaScript
interface obj {
name: string;
role: string;
}
let arr: obj[] = [
{
name: 'GFG User 1',
role: 'Developer'
},
{
name: 'GFG User 2',
role: 'Designer'
},
{
name: 'GFG User 3',
role: 'Tester'
}
];
arr.forEach(data => {
console.log
(
`Name: ${data.name},
Role: ${data.role}`
);
});
Output:
Name: GFG User 1, Role: Developer
Name: GFG User 2, Role: Designer
Name: GFG User 3, Role: Tester
Using map method
In this approach, we are using the map method on an array of objects (arr) in TypeScript to iterate over each object, prints its properties (name and role), and return the original data.
Syntax:
const newArray = array.map((element: ElementType, index: number, array: ElementType[]) => {
// code
});
Example: The below example uses map method to iterate an array of objects in TypeScript.
JavaScript
interface obj {
name: string;
role: string;
}
let arr: obj[] =
[
{
name: 'GFG User 1',
role: 'Developer'
},
{
name: 'GFG User 2',
role: 'Designer'
},
{
name: 'GFG User 3',
role: 'Tester'
}
];
arr.map(data => {
console.log
(
`Name: ${data.name},
Role: ${data.role}`
);
return data;
});
Output:
Name: GFG User 1, Role: Developer
Name: GFG User 2, Role: Designer
Name: GFG User 3, Role: Tester
Using reduce Method
Another approach to iterate over an array of objects in TypeScript is by using the reduce method. This method can be particularly useful when you need to accumulate or aggregate data from the array. The reduce method applies a function against an accumulator and each element in the array to reduce it to a single value.
Example: The below example uses the reduce method to iterate over an array of objects in TypeScript and accumulates the names and roles into a new array of strings.
JavaScript
interface User {
name: string;
role: string;
}
const users: User[] = [
{ name: "GFG User 1", role: "Developer" },
{ name: "GFG User 2", role: "Designer" },
{ name: "GFG User 3", role: "Tester" }
];
const userDescriptions = users.reduce((descriptions: string[], user: User) => {
descriptions.push(`Name: ${user.name}, Role: ${user.role}`);
return descriptions;
}, []);
console.log(userDescriptions.join('\n'));
Output:
Name: GFG User 1, Role: Developer
Name: GFG User 2, Role: Designer
Name: GFG User 3, Role: Tester
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
JavaScript Interview Questions and Answers JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. Both front-end and back-end developers need to have a strong command of JavaScript, as
15+ min read
React Tutorial React is a JavaScript Library known for front-end development (or user interface). It is popular due to its component-based architecture, Single Page Applications (SPAs), and Virtual DOM for building web applications that are fast, efficient, and scalable.Applications are built using reusable compon
8 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read