0

Say I have a string as:

const subject = "This process is flawless"

and I have an array as:

const matchArray = ["process","procedure","job"]

I want it such that if subject contains any keyword from matchArray,

if (subject matches any keyword of matchArray ){

console.log('true')
}

My first instinct was to use includes but I don't want to match array with a string but instead string with an array.

I'm still exploring, if anyone could guide me that would be reall helpful.

Edit: I found this soultion, but is there any better solution than this

const subject = "This process is flawless"
const matchArray = ["process","procedure","job"]
const exists = matchArray.some(matchArray => subject.includes(matchArray))

if (exists) {
  console.log("Yes");
  // notify 
}
Sushant Rad
  • 249
  • 2
  • 10

2 Answers2

1

Using some()

const subject = "This process is flawless"
const matchArray = ["process", "procedure", "job"]

console.log(matchArray.some(i => subject.includes(i)))
User863
  • 19,346
  • 2
  • 17
  • 41
0

Here is my code if you want to check separately each index of array if it is present or not

const subject = "This process is flawless"
const matchArray = ["process","procedure","job"]
const subject_array=subject.split(" ");
console.log(subject_array);// split the string by space
// loop on matchArray matchArray
for(i=0;i<matchArray.length;i++){
const c_string=matchArray[i];
var check = subject.includes(c_string);
if(check==true){
 console.log(c_string,' is present');
    }
  }

Here is the out put

Moman Raza
  • 155
  • 1
  • 5