类成员变量是动态的.取决于typescript中映射的键



我有一个typescript中的映射myMap:Map=new Map((;

myMap有一些键&值

我正在迭代下面的地图

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
}

例如,如果地图的键是===>苹果、芒果等如果对应的值是1,2然后在迭代地图的同时动态创建

myClass {
apple = 1;
mango = 2;
}

你能试试这个吗

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

最新更新