【问题标题】:Store the word found in a array as a variable将在数组中找到的单词存储为变量
【发布时间】:2021-05-10 01:32:06
【问题描述】:
let a = process.env.a
   for(let word of a){
    if(message.content.toLowerCase().includes(word)){
      var vs = //The Word Found In A Discord Message
      message.reply("Said " + "'" + vs + "'")
      break;
      }
  }

如何将不和谐消息中找到的单词存储为 VS

【问题讨论】:

  • 嗯...word 不包含您要查找的单词吗?这就是使用浏览器内置的调试器的好主意。
  • 它的nodejs...我不能那样调试。
  • @LockdownGaming yes you can
  • 请提供process.env.amessage.content 的示例,因为显然word 实际上并不包含您要查找的单词。
  • message.content 是来自 discord 服务器的消息,process.env.a 是 ['badword1', 'badword2']

标签: javascript node.js arrays environment-variables discord.js


【解决方案1】:

word 包含您要查找的单词,因此请发送该单词而不是使用其他变量。

【讨论】:

  • 出于某种原因,它只给了我它的第一个字母。
  • 你能显示你在 process.env.a 中有什么
  • 示例:['badword1', 'badword2']
  • 我怀疑process.env.a 有一个数组,它可能是一个字符串。
  • 看起来像上面的例子。
【解决方案2】:

您的word 变量存储您需要的值。

如果您的意思是,您需要找到一些具有特定特征的变量,您可以使用下面的示例。

  • 此代码查找数组中长度大于 2(不等于)的第一个元素:
    const arr = ['aa', 'bbb', 'cccc'];

    const word = arr.find(elem => elem.length > 2);
  • 此代码在数组persons 中查找第一个对象,该对象的age 变量大于18(不等于)。
    const persons = [
      {
        name: 'Alex',
        age: 18
      },
      {
        name: 'Sardor',
        age: 20
      }
    ];
  
    const word = persons.find(person => person.age > 18);

阅读更多关于.find()

还可以查看.filter()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-10
    • 2022-12-19
    相关资源
    最近更新 更多