How to Iterate Array of Objects in TypeScript ?

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:

Table of Content

  • Using for…of Loop
  • Using forEach method
  • Using map method

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


Contact Us