【问题标题】:Getting All Elements In An Array (Javascript)获取数组中的所有元素(Javascript)
【发布时间】:2020-09-29 09:36:32
【问题描述】:

我正在尝试检查一个字符串是否包含我存储在数组中的某些单词......但是我对 javaScript 有点新,所以我不完全知道如何检查数组中的所有元素。 ..这是一个例子:

const fruits = ["apple", "banana", "orange"]

上面的数组只是一个例子,因为我实际上是在检查是否有人在聊天中发送脏话。

if(message.content.includes(fruits)){executed code};

我的问题是,当我检查水果时它什么也没做,但是当我检查数组中的特定元素时,如fruits[0] //returns apple 它实际上会检查... 所以我的问题/问题是如何检查数组中所有元素的字符串,而不仅仅是苹果。

【问题讨论】:

  • 这能回答你的问题吗? Determine whether an array contains a value
  • 您能澄清一下message.content 是什么吗?它是一个字符串还是一个数组?我的印象是,例如用户在一个线程中发布的一些评论,你想看看你的数组中是否有任何单词在其中找到,是吗?
  • 是的,抱歉,message.content 是一个字符串,它在 discord.js 中我已经添加了该标签但忘记澄清它

标签: javascript discord.js


【解决方案1】:

你对includes的用法是错误的。

来自 MDN:

includes() 方法确定数组是否在其条目中包含某个值,根据需要返回 true 或 false。

arr.includes(valueToFind[, fromIndex])

const fruits = ["apple", "banana", "orange"];
const swearWord = "orange";

// execute is available
if (fruits.includes(swearWord))
  console.log("Swear word exists.");
else 
  console.log("Swear word doesn't exist.");

反之,如果字符串包含数组的脏话:

const fruits = ["apple", "banana", "orange"];
const swearWord = "this contains a swear word. orange is the swear word";

// execute is available
if (checkForSwearWord())
  console.log("Swear word exists.");
else
  console.log("Swear word doesn't exist.");

function checkForSwearWord() {
  for (const fruit of fruits) 
    if (swearWord.includes(fruit)) return true;
  return false;
}

【讨论】:

  • 是的,但是......这确实适用于数组......我正在尝试检查一个字符串是否包含一个发誓的词而不是一个数组......我想如果有什么我可以使用 toarray 方法字符串然后按照您的建议进行操作。
  • @PandaDev 我已经更新了这两种情况的代码。如果对您有帮助,请点赞。 :)
  • 我已经投票了,因为您以前的代码也是一个非常有用的见解。这实际上比我以前使用的要好得多...谢谢我实际上将您的回答视为答案...再次感谢您!
  • 不客气。作为奖励,您可以在比较两个字符串时使用.toLowerCase()
  • 我知道...我已经在使用它,所以人们不能在脏话上使用大写技巧
【解决方案2】:

反之亦然。您必须在数据数组上使用 .includes 来检查数组是否包含您要查找的单词。

const fruits = ["apple", "banana", "orange"]
console.log(fruits.includes("banana"))
console.log(fruits.includes("something not in the array"))

【讨论】:

  • 我偷偷怀疑 OP 的 message.content 是很多任意文本,他们想查看是否在其中找到单词,而不是完全匹配或本身就是数组。
  • 谢谢,我最终发现大多数人的建议是正确的......事实上我正在检查数组的字符串,而我真的应该检查消息是否有数组中的任何内容
【解决方案3】:

我会在这里使用交叉点。以防万一你不知道那是什么......

交集是两个数组共有的元素。

例如

swearWords = ["f***", "s***"];
messageWords = ["I", "am", "angry...", "f***", "and", "s***"];
let intersection = messageWords.filter(x => swearWords.includes(x));
console.log(intersection) //-> ["f***", "s***"]

【讨论】:

  • 感谢您的输入...我稍后将在代码中使用它,但我最终使用了一种更简单的方法,即检查字符串中的数组内容而不是数组中的字符串内容。 . 换句话说,我使用了 array.includes 而不是 string.includes 方法。
【解决方案4】:

反转它:

if(fruits.includes(message.content)){executed code};

Array.includes 的文档。您正在使用String.includes 方法。或者,您也可以使用indexOf 方法。

【讨论】:

  • 我会很快尝试这个,因为我相信我按照你的建议倒退了
  • 这最终成为了最好的,谢谢你的帮助,我知道数组有这个方法只是不太确定如何使用它,直到你指出它,再次谢谢你!!!
【解决方案5】:

试试这个。

fruits.forEach(fruit => {
    if (message.content.includes(fruit)) {
        console.log('true')
        return;
    }
    console.log('false')
})

希望对您有所帮助。

【讨论】:

  • 是的,这行得通,但是我最终使用了数组 .includes 的方法,而不是字符串 .includes 的方法,而且它工作起来更容易一些……谢谢你的输入!!!
  • 会做的,我也尝试过我选择作为答案的那个,这没有像另一个那样的错误......另一个基本上必须将数组项作为第一部分字符串,这个方法几乎完美无缺
【解决方案6】:

你可以尝试使用Array some方法

const fruits = ["apple", "banana", "orange"]
const containsFruit = fruit => message.content.includes(fruit);

if(fruits.some(containsFruit)) { executed code }

containsFruit 是一个函数,如果在 message.content 中找到水果,则返回 true

fruits.some(containsFruit) 如果发现数组中的任何项目包含在 message.content 中,则为 true

【讨论】:

  • 这确实工作得非常好,但我最终选择了 array.includes 而不是 string.includes 并且它解决了我的问题......但是我会在未来询问数组之前参考这个答案再次,非常感谢!!!
【解决方案7】:

另一种方法是使用正则表达式。

对于较大的消息字符串,这可能比在循环中调用 .includes() 与数组(每次都需要遍历整个字符串)更快。但是,这应该进行测试。

let fruits = ["apple", "banana", "orange"]
let fruits_regex = fruits.map(f => f.replace(/(?=\W)/g, '\\')).join('|');  // escape any special chars
// fruits_regex == "apple|banana|orange"

let message = 'apple sauce with oranges';
let matches = [ ...message.matchAll(fruit_regex) ]
// [
//   ["apple",  index:  0, input: "apple sauce with oranges", groups: undefined]
//   ["orange", index: 17, input: "apple sauce with oranges", groups: undefined]
// ]

【讨论】:

    【解决方案8】:
    const fruits = ["apple", "banana", "orange"]
    if(fruits.some(x => message.content.includes(x))){
      /* executed code */
    };
    

    【讨论】:

    • 好的答案包含解释,而不仅仅是代码。请编辑解释,否则您的回答有被删除的风险。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-31
    • 1970-01-01
    相关资源
    最近更新 更多