0

Not seeing the error in my ways...

I am trying to search a string with keywords from an array and I just keep coming up with no results, please help me see what I am doing wrong here.

    var stringArray = ["Trans", "Diode", "Label"];
    var str = "Label, SpotChem Pipettes Oversticker";
    var a = (stringArray.indexOf(str) > -1);
    var b = (str.indexOf(stringArray) > -1);

    console.log("a: " + a + " b: " + b);

    //even using jquery: $.inArray(str, stringArray) returns -1
...

If needed you can see this code in a FIDDLE

georg
  • 211,518
  • 52
  • 313
  • 390
Devnsyde
  • 1,297
  • 1
  • 13
  • 34
  • Related: https://stackoverflow.com/questions/44169545/whats-a-fast-straight-forward-find-any-of-these-strings-in-this-text-for-jav#comment75398388_44169545 – le_m May 31 '17 at 18:14
  • You checking if any element in the array has the value of `"Label, SpotChem Pipettes Oversticker"`, which it doesn't. – Spencer Wieczorek May 31 '17 at 18:14

1 Answers1

3

You need to compare each word of the array to the string in question. You can use Array.some

var containsKeyWords = stringArray.some(word => str.indexOf(word) > -1);
tymeJV
  • 103,943
  • 14
  • 161
  • 157