Populate options in dropdown help

#1

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;
0 Likes

#2

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...

listArray[i-1] = { gblC.zones[i].name : i };
0 Likes

#3

Thanks, but I have tried exactly this syntax myself.

I get a syntax error in the function editor stating "expected ':' instead saw '.'"

Joe

0 Likes

#4

apologies (should indeed have been an : - edited above )
Though it also depends if your global array starts at 0 or 1.... so may need to be

 listArray[i-1] = { gblC.zones[i-1].name : i };
0 Likes

#5

Yup, tried that too. Now I get syntax error:

0 Likes

#6

Show us the function node from the beginning, so we can see which is line 9 and also so we can see the lines before.

0 Likes

#8

its in my original post, but here it is again. Line 9 is the line within the loop.

> var gblC = global.get("gblConfig");
> 
> var listArray = [];
> 
>     for (i=1;i<=3;i++) {
>         
>         //listArray[i] = gblC.zones[i].name;
>         listArray[i] = i;
>         //listArray[i-1] = { gblC.zones[i].name : i };
>     }
> 
> msg.options = listArray;
> 
> return msg;
0 Likes

#9

That isn't the one giving the syntax error. Show us what it is like now please

0 Likes

#10

Sorry, I am trying to continue working so I commented out the offending line. Here it is.

FYI, both of the commented out lines within the loop work. One gives me the zone names and the other just the zone numbers

var gblC = global.get("gblConfig");

var listArray = [];

    for (i=1;i<=3;i++) {
        
        //listArray[i] = gblC.zones[i].name;
        //listArray[i] = i;
        listArray[i-1] = { gblC.zones[i].name : i };
    }

msg.options = listArray;

return msg;
0 Likes

#11

Something like this then

        listArray[i-1] = {};
        listArray[i-1][gblC.zones[i].name] = i;
0 Likes

#12

Thanks! That syntax worked.

I understand the first line. Can you please explain the syntax in the 2nd line? I don't understand what the brackets around the zone name do?

Thanks

Joe

0 Likes

#13

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.

1 Like

#14

Thanks you!!

No wonder why I could never get it to work with the "."s.

0 Likes