PandaDev
PandaDev

Reputation: 131

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.

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.

Upvotes: 9

Views: 1507

Answers (8)

James McGuigan
James McGuigan

Reputation: 8086

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

Upvotes: 1

Mateusz Krawczyk
Mateusz Krawczyk

Reputation: 308

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

Upvotes: 0

Shravan Dhar
Shravan Dhar

Reputation: 1560

Your usage of includes is wrong.

From MDN:

The includes() method determines whether an array includes a certain value among its entries, returning true or false as appropriate.

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

Upvotes: 10

William Ku
William Ku

Reputation: 798

you can try to use the Array some method

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

fruits.some(containsFruit) will be true if any item in the array is found to be contained in the message.content

Upvotes: 2

cyberwombat
cyberwombat

Reputation: 40084

Reverse it:

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

Docs for Array.includes. You are using the String.includes method. You can, alternatively, also use the indexOf method.

Upvotes: 3

0p3r4t0r
0p3r4t0r

Reputation: 683

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.

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***"]

Upvotes: 3

holydragon
holydragon

Reputation: 6728

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.

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

Upvotes: 3

Arpit Vyas
Arpit Vyas

Reputation: 2227

try this .

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

Hope this help .

Upvotes: 2

Related Questions