I have a list and an object and I would like to use a list value to get one of the keys' values.
let devices = ['001A2208B97D','001A2208C9FA','001A2214ADC8','001A2214A73A','001A2214B86E','001A2214A6DF','001A2214ADBF','001A2208CFD3']
let entities = ['Temperature', 'Valve', 'Battery', 'Offset']
let temperature = { device_class: 'temperature', icon: "hass:thermometer-bluetooth", unit: "°C"}
let valve = { device_class: '', icon: "hass:valve", unit: "%"}
let battery = { device_class: 'battery', icon: "hass:battery-bluetooth", unit: ""}
let offset = { device_class: '', icon: "hass:offset", unit: "°C" }
for (let i = 0; i < devices.length; i++) {
for (let j = 0; j < entities.length; j++) {
msg.payload = {
"icon": temperature['icon'],
"unit_of_measurement": temperature['unit'],
"state_class": "measurement",
}
}
}
As you can see, the entities is lower case, so before getting the value from temperature, I need to convert to lower case.
I tried entities[0].toLowerCase()['unit'] and entities[0.toLowerCase()]['unit'] and (entities[0].toLowerCase())['unit'] and I have run out of ideas.
Does anybody know how to do this correctly? Ideally in a one liner, i.e. without first creating a new list or dict with all the lower case value. On the fly if possible :)
temperaturein it instead of having a variable? likeconst data = {"temperature" : { icon: "mdi:thermometer-bluetooth", unit: "°C" }}? If you can create that, it will be really easy for you to work with. Otherwise you have to use something like the unsafeevalto evaluate your string to the variable.temperatureis the name of a variable, you can access the unit liketemperature.unit. Its not clear from your question what you are trying to accesswindow[entities[0].toLowerCase()].unitto get the unit from the window variable calledtemperature.a JSON- you have an object. This is JSON