How to iterate dictionary in angular template?


In our angular application, from controller I have returned a dictionary. How can I display the same in html template using *ngFor?

Please anyone help to achieve the same


You can create a pipe that maps the object (dictionary) to an array
containing of type : { key: string,value: any }

Somthing like this:

@Pipe({name: 'mapToArray'})
export class MapToArray implements PipeTransform {
  transform(value, args:string[]) : any {
    let arr = [];
    for (let key in value) {
      arr.push({key: key, value: value[key]});
    return arr;

and use it like that in the html file:

<span *ngFor="let item of content | mapToArray">           
  Key: {{item.key}}, Value: {{item.value}}

Answered By – Ofek Amram

Answer Checked By – David Marino (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.