5

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.

4 Answers 4

9

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

Sign up to request clarification or add additional context in comments.

1 Comment

This is a common way and should be the accepted answer. But square bracket notation is an antipattern as it might introduce a security issue (leading to remote code execution). Using the eslint-plugin security/recommended this would lead to an error here. web.archive.org/web/20150430062816/https://blog.liftsecurity.io/…
4

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

Comments

0

You can use array like notation. Example:

htmldata[machine].temp

You can even use two variables:

htmldata[machine][anotherVariable]

A working example

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.