I am trying everything that I can think of to populate the options in a dropdown. I just cannot get it right.
I have a list of areas in the house that I call zones. Each zone has a different name. I want to iterate through the zone names and have those as the visible options in the dropdown. Which the code below accomplishes. But I also want the dropdown to return a number when selected and not the zone name. For example, I want the first zone name selection to return a 1. The second to return a 2, etc.
I have looked at the ui description for the dropdown, and also online example, but I cannot get the syntax correct in my code. I am sure it is a really easy fix but I just cannot put my finger on it. I sure would appreciate a little guidance here.
var gblC = global.get("gblConfig");
var listArray = [];
for (i=1;i<=2;i++) {
listArray[i] = gblC.zones[i].name;
}
msg.options = listArray;
return msg;
Close - as per the info each entry needs to be either just a string (used for both label and value ) or an object with a label and value - from the example - [ "Choice 1", "Choice 2", {"Choice 3":"3"} ]
so you need the object version...
a.b is actually a shorthand for a[b] meaning named element b of a. You can't use listArray[i-1].gblC.zones[i].name because that would first do listArray[i-1].gblc then .zones[i] and so on which is not what you want. You want it to evaluate gblC.zones[i].name first, then use that to determine which named element of listArray[i-1] to address. So you have to use the syntax as the dot shorthand method will not evaluate correctly.