简体   繁体   English

类成员变量是动态的。 取决于打字稿中地图的键

[英]Class member variable are dynamic. Depending upon the keys of a map in typescript

I have a map in typescript like myMap : Map = new Map();我有一个像 myMap 这样的打字稿地图: Map = new Map();

myMap having some keys & values myMap 有一些键和值

I am iterating the map like below我正在像下面这样迭代地图

for(let [key, value] of this.myMap) {
   ///some code which can create a class whose member variable will be the key of the map and the values of the member variables will be the values of the keys
}

eg if keys of map are ==> apple, mango etc if corresponding values are 1, 2 then create dynamically while iterating the map例如,如果地图的键是 ==> 苹果、芒果等,如果对应的值为 1、2,则在迭代地图时动态创建

myClass {
  apple = 1;
  mango = 2;
}

Can you try this你能试试这个吗

let myClass = {};
for (const [key, value] of this.myMap.entries()) {
  myClass = {
    ...myClass,
    [key]: value,
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM