-
• #2
Your issue is you're not defining
name
locally, so it's a global variable and just ends up filled with the last value you set it to. If you change:data = getDataStreams(); Object.keys(data).forEach(function(k){ name = data[k].description.split(" / ")[0];
to
data = getDataStreams(); Object.keys(data).forEach(function(k){ var name = data[k].description.split(" / ")[0];
Then
name
ends up defined locally in the function defined inside forEach, so each key in data has its own version ofname
set. -
• #3
That's exactly it, thank you!
I'm writing a menu with multiple boolean variable. The menu is populated on load, so I don't know ahead of time how many variables there are or what they are named. I am having an issue saving the value on change. My code is as follows:
the "data" variable looks like this:
The submenu populates like this:
The issue is that when the "onchange" function runs, name is set to the most recent loop of the foreach, herein "Temperature, water". However, I want it to reflect what the variable "name" was set to when the submenu item was added to the submenu object.
I could convert the function to a string, replace the variable with its value, and then convert back to function within each iteration of the loop, but that strikes me as messy. Does anyone know a better way of doing this?