Help! Dynamically create menu from an array

Hello!

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

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

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

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

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

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:

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
            }
        ]

    }
];

Again you provide no input.

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

Thanks, appreciate your help :slight_smile: . It works!

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