Angular 将变量指定为数组中的键

Angular 将变量指定为数组中的键,angular,typescript,Angular,Typescript,我在初始化数组时遇到问题。因此,基本上我使用库ngx gauge在我的应用程序中显示仪表,其中一个属性是阈值,根据值有三种不同的颜色。例如: thresholds={0':{color:'red'},'100':{color:'green'},'200':{color:'orange'} 我的问题是,我想把一个变量替换为“0”、“100”和“200”。我想做的是: const level1 = manager.getLevelOne(); const level2 = manager.getLe

我在初始化数组时遇到问题。因此,基本上我使用库ngx gauge在我的应用程序中显示仪表,其中一个属性是阈值,根据值有三种不同的颜色。例如:
thresholds={0':{color:'red'},'100':{color:'green'},'200':{color:'orange'}

我的问题是,我想把一个变量替换为“0”、“100”和“200”。我想做的是:

const level1 = manager.getLevelOne();
const level2 = manager.getLevelTwo();
const level3 = manager.getLevelThree();
thresholds ={ level1 : {color:'red'}, level2:{color:'green'}, level3:{color:'orange'}};

但是,当I console.log thresholds时,它将level1、2和3显示为别名,而不是它们的值。

之所以
thresholds={level1:{color:'red'}}
不起作用,是因为它相当于
thresholds={“level1”:color:'red'}
(带引号)。只要
level1
是一个合法的标识符,这两个版本在JavaScript中是等效的,第二个版本清楚地说明了发生了什么。
const level1=“0”;
const level2=“100”;
const level3=“200”;
阈值={
[level1]:{
颜色:“红色”
},
[2级]:{
颜色:“绿色”
},
[3级]:{
颜色:“橙色”
}
};

console.log(阈值)使用
阈值={};阈值[level1]={color:'red'};阈值[level2]={color:'green'}等可能重复的