我试图在Angular 4(Ionic 3)中存储一些值,然后根据键从数组中获取一行。键被传递给函数,函数检索用于计算值的值。类属性如下。我很确定它格式正确。
let chemicals = [{
"trichlor" : [{
"ozmul": 6854.95,
"volume": "x"
}],
"dichlor": [{
"ozmul": 4149.03,
"volume": 0.9351
}],
"cal-hypo-48": [{
"ozmul": 3565.44,
"volume": 0.9352
}],
"cal-hypo-53": [{
"ozmul": 3936.84,
"volume": 0.9352
}],
"cal-hypo-65": [{
"ozmul": 4828.12,
"volume": 0.9352
}],
"cal-hypo-73": [{
"ozmul": 5422.41,
"volume": 0.9352
}],
"lithium-hypo": [{
"ozmul": 2637.5,
"volume": 0.978
}],
"chlorine-gas": [{
"ozmul": 7489.4,
"volume": "x"
}]
}];
所以基本上,我需要做的是获取传递给函数的值(让我们以trichlor为例)。然后我需要能够使用该值来获取与trichlor相关的ozmul和体积值。在提供商内完成此任务的正确方法是什么?下面是使用值的函数的代码示例:
chemical = "trichlor"; // This is passed into the function
if (chemicals.chemical.ozmul == 'x') {
let tempValueTwoWeight = 'Unknown';
} else {
let tempValueTwoVolume = (tempValueTwo * chemicals.chemical.volume)
}
答案 0 :(得分:0)
只需使用:
chemicals[0][chemical][0].ozmul
代替chemicals.chemical.ozmul
答案 1 :(得分:0)
这样的事情也适合你,你不需要硬编码路径
test(val:string){
this.chemicals.forEach(data => {
let value = data[val].find(data => data.volume === 'x');
console.log(value);
});
}
将此作为方法传递您要搜索的val,并且data.volume参数可以动态化并检查