Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
88
rated 0 times [  91] [ 3]  / answers: 1 / hits: 194034  / 7 Years ago, wed, january 10, 2018, 12:00:00

I am new to angular 5 and trying to iterate the map containing another map in typescript.
How to iterate below this kind of map in angular
below is code for component:



import { Component, OnInit} from '@angular/core';

@Component({
selector: 'app-map',
templateUrl: './map.component.html',
styleUrls: ['./map.component.css']
})
export class MapComponent implements OnInit {
map = new Map<String, Map<String,String>>();
map1 = new Map<String, String>();

constructor() {


}

ngOnInit() {
this.map1.set(sss,sss);
this.map1.set(aaa,sss);
this.map1.set(sass,sss);
this.map1.set(xxx,sss);
this.map1.set(ss,sss);


this.map1.forEach((value: string, key: string) => {
console.log(key, value);

});


this.map.set(yoyoy,this.map1);

}



}


and its template html is :



<ul>
<li *ngFor=let recipient of map.keys()>
{{recipient}}
</li>


</ul>

<div>{{map.size}}</div>


runtime


More From » angular

 Answers
33

For Angular 6.1+ , you can use default pipe keyvalue ( Do review and upvote also ) :



<ul>
<li *ngFor=let recipient of map | keyvalue>
{{recipient.key}} --> {{recipient.value}}
</li>
</ul>


WORKING DEMO






For the previous version :



One simple solution to this is convert map to array : Array.from



Component Side :



map = new Map<String, String>();

constructor(){
this.map.set(sss,sss);
this.map.set(aaa,sss);
this.map.set(sass,sss);
this.map.set(xxx,sss);
this.map.set(ss,sss);
this.map.forEach((value: string, key: string) => {
console.log(key, value);
});
}

getKeys(map){
return Array.from(map.keys());
}


Template Side :



<ul>
<li *ngFor=let recipient of getKeys(map)>
{{recipient}}
</li>
</ul>


WORKING DEMO


[#55492] Monday, January 8, 2018, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
giovanny

Total Points: 314
Total Questions: 101
Total Answers: 90

Location: Tajikistan
Member since Thu, Apr 14, 2022
2 Years ago
;