MattG
MattG

Reputation: 1932

How to access dictionary values using a variable: Javascript

How can I access a dictionary value using a for loop and an array?

For example: I have a dictionary:

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

I know I can access values by using console.log(dict.machine_41.temp) However, how can I do this with a for loop, and an array?

I have tried:

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

I was hoping it would substitute machine for machine_41, and so on.

Upvotes: 5

Views: 52766

Answers (4)

Ele
Ele

Reputation: 33726

You can use the function Object.keys to gather the keys of an object, with that array you can loop and access the objects.

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

Or, you can use a simple for-loop along with the operator 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; }

If you want to use an array of numbers related to machines, you can use the function 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));

Upvotes: 4

Osama
Osama

Reputation: 3040

Use dict[machime] to refer to the property you want

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

Upvotes: 0

Ariel
Ariel

Reputation: 1436

You can use array like notation. Example:

htmldata[machine].temp

You can even use two variables:

htmldata[machine][anotherVariable]

A working example

Upvotes: 0

Dacre Denny
Dacre Denny

Reputation: 30360

You could do the following:

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

Upvotes: 9

Related Questions