I have a array, let's say:
var myArray = ["ibira", "garmin", "hide", "park", "parque", "corrida", "trote", "personal", "sports", "esportes", "health", "saúde", "academia"];
var myString = "I went to the park with my garmin watch";
What is the fast way to check if my String has any of the words in myArray?
Bellow is my code, but im not sure if it would be the best way to go...
function score(arKeywords, frase) {
if (frase == undefined) {
return 0;
} else {
var indice = 0;
var indArray = arKeywords.length;
var sentencaMin = frase.toLowerCase();
for (i = 0; i < indArray; i++) {
if (sentencaMin.search(arKeywords[i]) > 0) { indice++; }
}
return indice;
}
}
Please help me anyone. That function will run in A LOT of strings!
Thank you all :)
myString.split(/\s+/).filter(Set.prototype.has, new Set(myArray))(or usesomeinstead offilterto determine "whether" not "which").