如何使用 for 循环和数组访问字典值?
例如:我有一本字典:
var dict = {
machine_41: {
temp: "14",
humidity: "89",
time: Wed Oct 31 2018 12: 27: 16 GMT - 0500(Central Daylight Time)
},
machine_42: {
temp: "20",
humidity: "13",
time: Wed Oct 31 2018 12: 27: 41 GMT - 0500(Central Daylight Time)
},
machine_43: {
temp: "34",
humidity: "36",
time: Wed Oct 31 2018 1 GMT - 0500(Central Daylight Time)
}
我知道我可以通过使用访问值 console.log(dict.machine_41.temp)
但是,如何使用 for 循环和数组执行此操作?
我努力了:
let activeMachines = [41,43,45];
for(let i = 0; i < activeMachines.length; i++){
let machine = ('machine_'+ activeMachines[i]);
console.log(htmldata.machine.temp);
}
我希望它能用 machine
代替 machine_41
,等等。
原文由 MattG 发布,翻译遵循 CC BY-SA 4.0 许可协议
您可以执行以下操作: