Iterating over Typescript Map

前端 未结 12 1782
无人共我
无人共我 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:43

    If you don't really like nested functions, you can also iterate over the keys:

    myMap : Map;
    for(let key of myMap) {
       if (myMap.hasOwnProperty(key)) {
           console.log(JSON.stringify({key: key, value: myMap[key]}));
       }
    }
    

    Note, you have to filter out the non-key iterations with the hasOwnProperty, if you don't do this, you get a warning or an error.

提交回复
热议问题