Retrieve array number from a dropdown list

Hello everyone,

So, I am creating a dropdown list from a CSV file and I want to be able to retrieve the array number of the selected Item but after trying different options I don't really know how I can do It.

Would mean a lot if you can help me :slight_smile:

(on the two photos you can see first my nodes and secondly I tried to explain what I'm tryin to do with a drawing)

Hello ..

it would be better if you provided some sample data from your csv or shared what you have tried in those Function nodes.

Based on the Dropdown node's help tab .. it mentions that if you populate the dropdown dynamically using the msg.options property .. you can send to it an array of objects where the property of the object is the label and the value will be the value you want the dropdown selection to output

Here is an example flow (an example is better than a thousand words) :wink:

[{"id":"55f9517b34767b6f","type":"ui_dropdown","z":"54efb553244c241f","name":"","label":"","tooltip":"","place":"Select option","group":"334da6559b3ac2c1","order":0,"width":0,"height":0,"passthru":true,"multiple":false,"options":[{"label":"","value":"","type":"str"}],"payload":"","topic":"topic","topicType":"msg","className":"","x":880,"y":700,"wires":[["678e2a4b43fda770"]]},{"id":"9df4a85d1374f6a4","type":"function","z":"54efb553244c241f","name":"function 1","func":"msg.options = msg.payload.map((el, index) => {\n\nreturn { [el.label] : index }\n    \n})\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":720,"y":700,"wires":[["55f9517b34767b6f","af0ba126a2079978"]]},{"id":"325697471bb636cd","type":"inject","z":"54efb553244c241f","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":120,"y":700,"wires":[["e9a8557891543add"]]},{"id":"e9a8557891543add","type":"template","z":"54efb553244c241f","name":"csv fake data","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"fan1, started\nfan2, stopped\nlight1, on\nlight2, off","output":"str","x":340,"y":700,"wires":[["f7a9fcabbaf023cc"]]},{"id":"f7a9fcabbaf023cc","type":"csv","z":"54efb553244c241f","name":"","sep":",","hdrin":"","hdrout":"none","multi":"mult","ret":"\\n","temp":"label","skip":"0","strings":true,"include_empty_strings":"","include_null_values":"","x":550,"y":700,"wires":[["9df4a85d1374f6a4","354e336913974590"]]},{"id":"354e336913974590","type":"debug","z":"54efb553244c241f","name":"debug 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":680,"y":620,"wires":[]},{"id":"af0ba126a2079978","type":"debug","z":"54efb553244c241f","name":"debug 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":880,"y":620,"wires":[]},{"id":"678e2a4b43fda770","type":"debug","z":"54efb553244c241f","name":"debug 3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1060,"y":700,"wires":[]},{"id":"334da6559b3ac2c1","type":"ui_group","name":"Default","tab":"ecf6721e62c24085","order":1,"disp":true,"width":"6","collapse":false,"className":""},{"id":"ecf6721e62c24085","type":"ui_tab","name":"Home","icon":"dashboard","disabled":false,"hidden":false}]
1 Like

When you create the msg.options set the array like this, {"name" : array index}

[
  {"processor name": 0},
  {"processor nĂ me2": 1}
]

Then the array index will be passed instead of name.

Sorry, I didn't send my flow because it's confidential and the industry I'm working for don't want me to share it. But thanks, it worked :smile: . I was using the msg.option badly , thinking I could put everything in a table to then retrieve the array number (I'm new to node-red).

Here's my old code:

image

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.