Home > Software design >  How can I check elements in an array that are found in a string?
How can I check elements in an array that are found in a string?

Time:01-11

I want to loop through an array of phrases and compare the elements with a given string, then create a new array of common elements. I'm pretty new to Javascript. The answers I came across from my research do not address my specific need.

I intend to loop through myArray and check elements in it that are found in userInput and store them in the intersect variable as a new array. I intend to get something like this: ["morning", "good", "good morning"] as the result, but I don't seem to get how to go about it rightly. Below is the code I tried. Like I said, I'm pretty new to Javascript.

var userInput, myArray, intersect;
userInput "hey good morning bro";
myArray = ["today", "morning", "good", "good morning"];

for (i=0;i<myArray.length;i  ){
intersect = userInput.includes(i);
document.write(intersect);
}
}

CodePudding user response:

Loop through myArray with the filter method. This method is part of a toolkit that is found on every array.

The filter method loops over every item in your array, in this case being the strings like "today", "morning", etc. It will check for each string if it is found in the userInput string. If it is, then true is returned, meaning the word can be part of the new array. If the result is false then the word will not be included in the new array.

const userInput = "hey good morning bro";
const myArray = ["today", "morning", "good", "good morning"];

const intersect = myArray.filter(word => 
  userInput.includes(word)  
);
  
console.log(intersect);

CodePudding user response:

Currently you check if the userInputs include an integer (index). you have to change it like below:

var userInput, myArray, intersect;
userInput = "hey good morning bro";
myArray = ["today", "morning", "good", "good morning"];

for (i=0; i<myArray.length; i  )
{
  intersect = userInput.includes(myArray[i]);
  document.write(intersect   ' => '   myArray[i]   " #</br> ");
}

CodePudding user response:

Split user input using the delimiter " ".

const userInput =  "hey good morning bro";
const myArray = ["today", "morning", "good", "good morning"];

let intersection = [];
const stringParts = userInput.split(" ");

for (let i = 0; i < myArray.length; i  ) {
     if (stringParts.includes(myArray[i])) {
          intersection.push(myArray[i]);
     }
}

console.log(intersection);

Note that the output will appear in the console, but you can change console.log if you want (although I recommend console.log for testing).

Also, do not use var. Use const for anything that won't change and use let for anything else.

CodePudding user response:

Try this code to get an array of common elements:

var userInput = "hey good morning bro";
var myArray = ["today", "morning", "good", "good morning"];
var intersect = [];

$.each(myArray, function(index, val) {
    if(userInput.indexOf(val) != -1){
        intersect.push(val);
    }
});

console.log(intersect);
  • Related