Iterating over Typescript Map

前端 未结 12 1816
无人共我
无人共我 2020-12-07 16:26

I\'m trying to iterate over a typescript map but I keep getting errors and I could not find any solution yet for such a trivial problem.

My code is:

         


        
12条回答
  •  无人及你
    2020-12-07 16:38

    Just a simple explanation to use it in an HTML document.

    If you have a Map of types (key, array) then you initialise the array this way:

    public cityShop: Map = new Map();
    

    And to iterate over it, you create an array from key values.

    Just use it as an array as in:

    keys = Array.from(this.cityShop.keys());
    

    Then, in HTML, you can use:

    *ngFor="let key of keys"
    

    Inside this loop, you just get the array value with:

    this.cityShop.get(key)
    

    Done!

提交回复
热议问题