I'm looking for a way to check any text string (boolean) if a word is present using an array. Currently, I'm using include() method for each keyword i'm after, thus creating a lot of inefficiencies. I figured using a for loop will help streamline the process but I'm running into a snag, by where my return value is multi-line.
I can't seem to figure out why I can't convert the iteration result to a text string. I've tried .Replace(/\r?\n|\r/), toString() and join(), but have no luck.
function searchForKeywords() {
let textString = 'This is a sample text string that contains a bunch of words, such as Semi and Metal. Happy searching!'
let keywords = ['Clear', 'Metal', 'Silver', 'Semi']
for (let i = 0; i < keywords.length; i++) {
let a = (textString.includes(keywords[i]));
let result = a.toString()
console.log(result);
}
}
searchForKeywords();
The result i'm getting from the above code is as follows:
false
true
false
true
The desired result will be: false, true, false, true
Just to clarify, when I replace toString() with join() and .Replace(/\r?\n|\r/), I get
TypeError: a.join is not a function || TypeError: a.join is not a function
I'm not sure why I get the error and as a noob and learning all this without someone to ask in real-time is quite frustrating. I'm sure a lot of people go through this... so please be gentle with me :-)
Any help is greatly appreciated. Thank you in advance.