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

时间:2018-10-31 22:54:59

标签: 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,依此类推。

5 个答案:

答案 0 :(得分:3)

您可以执行以下操作:

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

答案 1 :(得分:0)

您可以使用类似于数组的表示法。示例:

htmldata[machine].temp

您甚至可以使用两个变量:

htmldata[machine][anotherVariable]

有效的example

答案 2 :(得分:0)

尝试一下!

let activeMachines = [41,43,45];
for(let i = 0; i < activeMachines.length; i++){
    let machine = ('machine_'+ activeMachines[i]);
    console.log(htmldata[machine].temp);
}

答案 3 :(得分:0)

使用dict [machime]引用所需的属性

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(let i = 0; i < activeMachines.length; i++){
          let machine = ('machine_'+ activeMachines[i]);
          console.log(dict[machine]);
       }

答案 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));