Maryam
Maryam

Reputation: 1071

iterating through Map object using angular2 ngFor

I have a hashed map object that the key is a string representing a class number and the value for each key is an Arraylist of Student object so in general the object that I want to iterate through is of type: Map<String,Arraylist<Student>> I want to display a tree like structure of the object values, I tried to use :

<div *ngFor="let key of MyObject">{{key}} </div> But this shows only the class number but it does not show the students names in each class. does anyone know how to iterate through each key and each object inside that key?

Upvotes: 1

Views: 3097

Answers (1)

Mario Gomez Martin
Mario Gomez Martin

Reputation: 11

You should create another *ngFor to iterate through the arrayList

<div *ngFor="let key of MyObject"> Class {{key}}
  Students:
  <div *ngFor="let student of MyObject.get(key)">
    {{student.name}}
  </div> 
</div>

this way it should display each class and each student in that class.

Upvotes: 1

Related Questions