Generate msg.options for dropdown , search function

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

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.

This will achieve that output...

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

Just for info, map has an an index

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

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 :slight_smile:

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