打字稿:访问打字稿中键值对的属性

Typescript: Accessing the properties of a key value pair in typescript

我在打字稿中有一个键值对对象。

const data = {INTERNAL:[372042],EXTERNAL:[332244]};

上面不是地图,但我想遍历它的键并在 switch case 模式中使用它。

mapData(data: { key: string; value: Array<number> }):void{
switch(data[key]) {
case 'INTERNAL':
      console.log('this is Internal data' =+data[value];
break;
 case 'EXTERNAL':
      console.log('this is External data' =+data[value];
break;
default:
  }
}

data[key]data[value] 抛出错误 key/value is not existing'.

访问数据的正确方法是什么。

您提供的代码中没有迭代。这是你想要达到的目标吗?

for (key in data) {
  mapData({key: key, value: data[key]}); }