Janier
Janier

Reputation: 4452

Set a particular propert of all objects in an array in typescript

I have the below studentList

private studentList:Array<Student>

The Student class has a many properties and one of the property is 'status'. How can i set this 'status' property to a particular value for all the Students in the StudentList array.

I achieved this by doing forEach and setting it .

Is there a better way of doing this? May be avoid the iteration .

Upvotes: 0

Views: 42

Answers (2)

basarat
basarat

Reputation: 276383

How can i set this 'status' property to a particular value for all the Students in the StudentList array.

Simple forEach would do it.

Example

studentList.forEach(stu => stu.status = 'the value you want');

Upvotes: 1

Chris Reynolds
Chris Reynolds

Reputation: 5503

forEach is probably best for a status change. However, if it is an initial value, you are better embedded that as a default inside your Student class.

Upvotes: 0

Related Questions