NodeJS 选择菜单不一致JS|如何在.addOptions中使用数组?

mm9b1k5b  于 2022-12-22  发布在  Node.js
关注(0)|答案(2)|浏览(147)

我想在.addOptions中Map一个数组。
基本上,我想通过获取对象或数组的值,将值{ }添加到addOptions
示例:

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

ut6juiuv1#

map函数中缺少花括号。该函数应如下所示

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 }}))
93ze6v8z

93ze6v8z2#

值应该是字符串的形式,而不是整型或双精度型,不管你给的是什么,都要对它们使用toString()。

const row = new ActionRowBuilder().addComponents(
            new StringSelectMenuBuilder()
                .setCustomId('select')
                .setPlaceholder('Nothing selected')
                .addOptions(
                    adana.map(function (curentValue, index) {
                        return {
                            label: index.toString(),
                            value: index.toString(),
                        }
                    })
                )
        )

相关问题