好几天我都想不出如何让for循环迭代所有项,然后运行条件.因此,我希望它对频道(returned from mysql query)进行总体初始化,然后判断是否为真.当每个项通过循环时,它将运行条件句.当条件为真时,它将执行,但然后它会转移到下一步并执行else.我try 过映射,但可能是错误的,甚至try 将其转换成一个Python.我甚至开始弄乱消息集合.

它在JavaScript中工作,所以不确定类型脚本在做什么.非常感谢任何帮助.

export = {
  name: 'addfav',
  aliases: ['Addfav'],
  type: CommandTypes.PrefixCommand,
  channelWhitelist: ['1147233774938107966'],
  ownerOnly: true,
  async execute(message: Message): Promise<void> {
    if (!message.mentions.channels.map((m) => m).length) {
      await message.reply('Did you forget to add the channel?');
    } else {
      let getChannel = await message.content.replace(/\D/g, '');
      let isAdded = await checkfav(message.author.id);
      for (var i = 0; i < isAdded.length; i++) {
        if (isAdded[i].channel === getChannel) {
          await message.reply('You already have a channel');
          break;
        } else {
          await addfav(getChannel);
          await message.reply('Channel added');
        }
      }
    }
  },
};

推荐答案

在您的场景中,您不需要搜索喜欢的频道.您只需判断频道是否已处于Collection 夹中即可.您可以使用some来实现它:

export = {
  name: 'addfav',
  aliases: ['Addfav'],
  type: CommandTypes.PrefixCommand,
  channelWhitelist: ['1147233774938107966'],
  ownerOnly: true,
  async execute(message: Message): Promise<void> {
    if (!message.mentions.channels.map((m) => m).length) {
      await message.reply('Did you forget to add the channel?');
      return;
    }
    let channelName = await message.content.replace(/\D/g, '');
    // get favaorited channels
    const favoritedChannels = await checkfav(message.author.id);
    const isFavorite = favoritedChannels.some((fav) => fav.channel === channelName)  
    if(isFavorite) {
      await message.reply('You already have this channel favorited');
      return;
    }
    await addfav(channelName);
    await message.reply('Channel added');
  },
};

然而,如果您确实需要迭代数组并对每个元素运行Expressc函数,则可以这样做:

  const favoritedChannels = await checkfav(message.author.id);
  for await (const channel of favoritedChannels) {
    await doSomething(channel);
  }

或者您可以并行运行:

  const favoritedChannels = await checkfav(message.author.id);
  const results = await Promise.all(favoritedChannels.map(channel => doSomething(channel));

Typescript相关问答推荐

为什么AB和CD这两种类型在TypScript中可以相互分配?

创建一个通用上下文,允许正确推断其中的子项

为什么ESLint抱怨通用对象类型?

带有微前端的动态React路由问题

为什么TypeScript假设文档对象始终可用?

使用`renderToPipeableStream`时如何正确恢复服务器端Apollo缓存?

类型{}上不存在属性&STORE&A;-MobX&A;REACT&A;TYPE脚本

Typescript泛型类型:按其嵌套属性映射记录列表

学习Angular :无法读取未定义的属性(正在读取推送)

如何在Angular 12中创建DisplayBlock组件?

尽管对象的类型已声明,但未解析的变量<;变量

如何从一个泛型类型推断多个类型

tailwind css未被应用-react

类型实例化过深,可能是无限的&TypeScrip中的TupleUnion错误

扩展对象的此内容(&Q;)

如何从JWT Reaction中提取特定值

使用泛型以自引用方式静态键入记录的键

映射类型不是数组类型

如何正确键入泛型相对记录值类型?

Svelte+EsBuild+Deno-未捕获类型错误:无法读取未定义的属性(读取';$$';)