如何使用变量访问字典值:Javascript

新手上路,请多包涵

如何使用 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 许可协议

阅读 438
2 个回答

您可以执行以下操作:

 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)'
    }
}

let activeMachines = [41,43,45];

for(const machineNumber of activeMachines) {

    let machineKey = `machine_${ machineNumber }`
    let machine = dict[ machineKey ]

    if(machine) {
      console.log(`tempurature for machine ${ machineNumber}: ${ machine.temp }`);
    }
}

原文由 Dacre Denny 发布,翻译遵循 CC BY-SA 4.0 许可协议

您可以使用函数 Object.keys 来收集对象的键,使用该数组您可以循环访问对象。

 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)"  }};

Object.keys(dict).forEach(k => {
  console.log(k, ':', dict[k].temp);
});
 .as-console-wrapper { max-height: 100% !important; top: 0; }

_或者,您可以使用简单的 for-loop 以及运算符 in_

 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)"  }};

for (let key in dict) {
  console.log(key, ':', dict[key].temp);
}
 .as-console-wrapper { max-height: 100% !important; top: 0; }

_如果要使用与机器相关的数字数组,可以使用函数 Array.prototype.forEach_

 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)"  }},
    activeMachines = [41,43,45];

activeMachines.forEach(m => console.log(`machine_${m}`, ':', (dict[`machine_${m}`] || {temp: 'Temperature not found.'}).temp));

原文由 Ele 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题