Convert one object list to another in Typescript

Suppose I have a list of student information as follows:

with the Student class with the following content:

If I now define another class StudentInfo that also contains student information with more information about the class as follows:

To convert the list of Student objects above into a list of StudentInfo objects, you can use the map() method as follows:

The map() method will process each element of the array. In the above example, for each element of the “students” array, we will initialize a StudentInfo object with properties whose values are derived from the Student object. Here, I am assigning the default class information to “A”, depending on your needs, you can write the code accordingly.

The result when I run this example is as follows:

Add Comment