Home > Software design >  Select Menu Discord JS | How can i use array in .addOptions?
Select Menu Discord JS | How can i use array in .addOptions?

Time:08-27

I would like to map an array inside .addOptions. Basically, I would like to add a value { } to addOptions by taking the values of an object or array.

Exemples :

        let menu = new SelectMenuBuilder()
                    .setCustomId('select')
                    .setPlaceholder('Nothing selected')
           .addOptions(
            arrayt.map(song => label: song.name, description: song.formattedDuration, value: blabla  )
 

CodePudding user response:

You are missing curly brackets in your map function. The function should look something like this

const songs = [
   {
     name: 'Song 1',
     formattedDuration: 2
   },
   {
     name: 'Song 2',
     formattedDuration: 5
   },
   {
     name: 'Song 3',
     formattedDuration: 6
   }
]

const menu = new SelectMenuBuilder()
      .setCustomId('select')
      .setPlaceholder('Nothing selected')
      .addOptions(songs.map(song => { return {label: song.name, description: song.formattedDuration }}))
  • Related