# Help! Dynamically create menu from an array

**URL:** <https://discourse.nodered.org/t/help-dynamically-create-menu-from-an-array/80536>\
**Category:** General\
**Created:** [13 August 2023 15:05 UTC](https://discourse.nodered.org/t/help-dynamically-create-menu-from-an-array/80536 "2023-08-13T15:05:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joe](https://avatars.discourse-cdn.com/v4/letter/j/b9e5f3/32.png) [@Joe](https://discourse.nodered.org/u/Joe)\
**Post date:** [13 August 2023 15:05 UTC](https://discourse.nodered.org/t/help-dynamically-create-menu-from-an-array/80536/1 "2023-08-13T15:05:37Z")

</div>

Hello!

I wonder if it's possible to dynamically create menu from an array:  
I wrote the following code, obviously it doesn't work:

```auto
let menuArray = msg.buildings;

for (let i = 0; i < menuArray.length; i++) {
  
var menu = [
    {
        "type": 1,
        "components": [
            {
                "type": 3,
                "custom_id": "class_select_1",
                 "options": [
                    {
                        "label": `${menuArray[i].name}`,
                        "value": `${menuArray[i].id}`,
                        }
                    },
                ],
                "placeholder": "Choose building",
                "min_values": 1,
                "max_values": 1
            }
        ]

    }
];

```

Thanks

---

<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:** [13 August 2023 16:34 UTC](https://discourse.nodered.org/t/help-dynamically-create-menu-from-an-array/80536/2 "2023-08-13T16:34:12Z")

</div>

There are many ways to do this  
But first you need to show us the input object and then an example of what the output would be for the input object.

What you have shown is to vague.

do you want an array of objects in options array or should the menu array be an array of objects.

If first this may help, but if not please supply an input and expected output

```auto
let menuArray = msg.buildings;
const menu = [
    {
        "type": 1,
        "components": [
            {
                "type": 3,
                "custom_id": "class_select_1",
                 "options": [
                    
                ],
                "placeholder": "Choose building",
                "min_values": 1,
                "max_values": 1
            }
        ]

    }
];

for (let i = 0; i < menuArray.length; i++) {
    let obj = {
        "option": menuArray[i].name,
        "value": menuArray[i].id
    }
    menu[0].components[0].options.push(obj);               
}

```

[edit] fixed typo

---

<div class="post-metadata">

**Author:** ![Joe](https://avatars.discourse-cdn.com/v4/letter/j/b9e5f3/32.png) [@Joe](https://discourse.nodered.org/u/Joe)\
**Post date:** [13 August 2023 17:04 UTC](https://discourse.nodered.org/t/help-dynamically-create-menu-from-an-array/80536/3 "2023-08-13T17:04:15Z")

</div>

Thanks for help.  
Yea, I want an array of objects in options array. Options should be formed from the array that is formed dynamically.  
I've just test your code and got "TypeError: Cannot read properties of undefined (reading '0')"

Input

```auto
let newMessage = {};
newMessage.channel = msg.payload.channelId;
newMessage.payload = `<@!${msg.payload.user.id}> "Select building"` ;
newMessage.message = msg.payload.replyMessage.id;

newMessage.embed = {
    "title": `BUILDING MENU`,
    "fields": [
        {
            "name": `Main resources you have 🌿`,
            "value": `🍗 ${msg.payload.graphql.users[0].food}`
        },
    ],
};

let menuArray = msg.buildings;

const menu = [
    {
        "type": 1,
        "components": [
            {
                "type": 3,
                "custom_id": "class_select_1",
                "options": [
                ],
                "placeholder": "Choose building",
                "min_values": 1,
                "max_values": 1
            }
        ]

    }
];

for (let i = 0; i < menuArray.length; i++) {
    let obj = {
        "option": `${menuArray[i].name}`,
        "value": `${menuArray[i].id}`
    }
    menu.components[0].options.push(obj);
 
}
newMessage.components = menu;
return newMessage;

```

Expected output:

```auto
var ok = [
    {
        "type": 1,
        "components": [
            {
                "type": 3,
                "custom_id": "class_select_1",
                "options": [
                    {
                        "label": `Building 1`,
                        "value": `1`,
                    },
                    {
                       "label": `Building 2`,
                        "value": `2`,
                    },
                    {
                        "label": `Building 3`,
                        "value": `3`,
                    },
                    {
                        "label": `Building .......and so on`,
                        "value": `......`,
                    },
                ],
                "placeholder": "Choose building",
                "min_values": 1,
                "max_values": 1
            }
        ]

    }
];

```

---

<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:** [13 August 2023 17:06 UTC](https://discourse.nodered.org/t/help-dynamically-create-menu-from-an-array/80536/4 "2023-08-13T17:06:50Z")

</div>

Again you provide no input.

Typo  
`menu[0].components[0].options.push(obj);`

---

<div class="post-metadata">

**Author:** ![Joe](https://avatars.discourse-cdn.com/v4/letter/j/b9e5f3/32.png) [@Joe](https://discourse.nodered.org/u/Joe)\
**Post date:** [13 August 2023 17:27 UTC](https://discourse.nodered.org/t/help-dynamically-create-menu-from-an-array/80536/5 "2023-08-13T17:27:04Z")

</div>

> [@Joe](#):
>
> `label`

Thanks, appreciate your help 🙂 . It works!

---

<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:** [27 August 2023 17:27 UTC](https://discourse.nodered.org/t/help-dynamically-create-menu-from-an-array/80536/6 "2023-08-27T17:27:36Z")

</div>

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