# Generate msg.options for dropdown , search function

**URL:** <https://discourse.nodered.org/t/generate-msg-options-for-dropdown-search-function/53783>\
**Category:** General\
**Created:** [15 November 2021 18:59 UTC](https://discourse.nodered.org/t/generate-msg-options-for-dropdown-search-function/53783 "2021-11-15T18:59:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![xjust](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/xjust/32/39096_2.png) [@xjust](https://discourse.nodered.org/u/xjust)\
**Post date:** [15 November 2021 18:59 UTC](https://discourse.nodered.org/t/generate-msg-options-for-dropdown-search-function/53783/1 "2021-11-15T18:59:30Z")

</div>

Hi there,  
As a beginner, I am stuck a little bit. Hope anybody can help 🙂

I have the following arr variable used as a settings file:  
const arr = [  
{ "zona": "Zone1", "ip": [10, 11, 12], },  
{ "zona": "Zone2", "ip": [13, 14] },  
{ "zona": "Zone3", "ip": [15] },  
];  
Can anybody help me generate msg.options for the dropdown widget from this?  
I managed to show it using arr.map(function (x) { return x.zona; }); , but there are no values assigned to each name using this method.

I need to extract the following format from it [{ "Zone1" : "0" }, { "Zone2" : "1" }, { "Zone3", "2" }]

Also is there a way do a search function based on the first number in this array , to return the index number if found, Search for 10,13 or 15 returns 0,1 or 2.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [15 November 2021 21:01 UTC](https://discourse.nodered.org/t/generate-msg-options-for-dropdown-search-function/53783/2 "2021-11-15T21:01:18Z")

</div>

This will achieve that output...

```auto
const arr = [
    { "zona": "Zone1", "ip": [10, 11, 12], },
    { "zona": "Zone2", "ip": [13, 14] },
    { "zona": "Zone3", "ip": [15] },
];

let index = 0;
msg.payload = arr.map(e => {
    return {
        [e.zona]: index++
    }
});

return msg;

```

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/9/19542c0db5774aec796779d932ce8fb816ce789d.png)

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [15 November 2021 21:39 UTC](https://discourse.nodered.org/t/generate-msg-options-for-dropdown-search-function/53783/3 "2021-11-15T21:39:34Z")

</div>

Just for info, map has an an index

```auto
msg.options = arr.map((element, index) => { return {[element.zona]:index+1} })

```

---

<div class="post-metadata">

**Author:** ![xjust](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/xjust/32/39096_2.png) [@xjust](https://discourse.nodered.org/u/xjust)\
**Post date:** [15 November 2021 21:49 UTC](https://discourse.nodered.org/t/generate-msg-options-for-dropdown-search-function/53783/4 "2021-11-15T21:49:20Z")

</div>

Thank you, it works just great.  
I will get quite a lot of requests / minute for that search function.  
Maybe it is better to create another variable at boot using the first value of the ip and search inside it ?  
changing [e.zona] to [e.ip[0]] ?  
Any other suggestions are welcome 🙂

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [14 January 2022 21:50 UTC](https://discourse.nodered.org/t/generate-msg-options-for-dropdown-search-function/53783/5 "2022-01-14T21:50:12Z")

</div>

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