简体   繁体   English

获取数组中的所有元素 (Javascript)

[英]Getting All Elements In An Array (Javascript)

I am trying to check if a string contains certain words which I had stored in an array... however I'm new to JS, so I don't exactly know how to check all of the elements inside the Array.我正在尝试检查一个字符串是否包含我存储在数组中的某些单词......但是我是 JS 的新手,所以我不知道如何检查数组中的所有元素。

Here is an Example:这是一个例子:

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

I am actually checking if someone sends swearwords in a chat.我实际上是在检查是否有人在聊天中发送脏话。

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

However my issue is when I check for fruits it does anything but when I check for a specific element in the array like fruits[0] //returns apple it will actually check for that... So my issue / question is how do I check the string for all of the elements in the array not just apples.但是我的问题是当我检查水果时它会做任何事情但是当我检查数组中的特定元素时fruits[0] //returns apple它实际上会检查那个......所以我的问题/问题是我如何检查数组中所有元素的字符串,而不仅仅是苹果。

Your usage of includes is wrong.您对includes的用法是错误的。

From MDN:来自 MDN:

The includes() method determines whether an array includes a certain value among its entries, returning true or false as appropriate. includes() 方法确定数组是否在其条目中包含某个值,并根据需要返回 true 或 false。

arr.includes(valueToFind[, fromIndex]) 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.");

To check the otherway, if string contains the array's swearword:要检查其他方式,如果字符串包含数组的脏话:

 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; }

You got it the other way around.你得到它相反的方式。 You have to use.includes on the data array to check if the array includes the word you are looking for.您必须在数据数组上使用 .includes 来检查数组是否包含您要查找的单词。

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

Reverse it:反转它:

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

Docs for Array.includes . Array.includes的文档。 You are using the String.includes method.您正在使用String.includes方法。 You can, alternatively, also use the indexOf method.或者,您也可以使用indexOf方法。

I would use an intersection here.我会在这里使用交叉路口。 Just in case you don't know what that is...以防万一你不知道那是什么......

An intersection is the elements that two arrays share in common.交集是两个 arrays 共有的元素。

For example例如

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

try this.试试这个。

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

Hope this help.希望这有帮助。

you can try to use the Array some method你可以尝试使用Array some方法

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

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

containsFruit is a function that will return true if a fruit is found in the message.content containsFruit 是一个 function,如果在 message.content 中找到水果,它将返回 true

fruits.some(containsFruit) will be true if any item in the array is found to be contained in the message.content如果发现数组中的任何项目包含在 message.content 中,fruits.some(containsFruit) 将为真

The other way to do this is with a regular expression.另一种方法是使用正则表达式。

For a large message string, this may be quicker than calling.includes() in a loop vs an array (which needs to loop over the entire string each time).对于大型消息字符串,这可能比在循环中调用 .includes() 与数组(每次都需要遍历整个字符串)更快。 However this should be tested.然而,这应该被测试。

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]
// ]

const fruits = ["apple", "banana", "orange"]
if(fruits.some(x => message.content.includes(x))){
  /* executed code */
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM