EN
TypeScript - forEach over array with objects
0 points
In this article, we would like to show you forEach()
method used to loop over an array with objects in TypeScript.
The forEach()
method calls provided function once for each element of an array. In this case, we have an array of objects.
Runnable example:
xxxxxxxxxx
1
interface User {
2
name: string;
3
age: number;
4
}
5
6
const users: User[] = [
7
{ name: 'Thomas', age: 21 },
8
{ name: 'Mark', age: 35 },
9
];
10
11
users.forEach((user: User) => {
12
console.log(user.name, user.age);
13
});
Output:
xxxxxxxxxx
1
Thomas 21
2
Mark 35