user2345351
user2345351

Reputation: 27

Checking if a string contains any part of an array element

I just started learning javascript and I'm working on small chrome extension that checks a certain website's item listings for any items that contain keywords provided by the user. I'm looking for some help on a method to compare a string to an array of strings.

So I have these variables:

var itemDetails = '[alter] saber 1/8 figure unopened';
var trackingList = ['alter figure', 'magic', 'sword art']; 

I need to check if itemDetails contains any of the strings in trackingList. I know I can use indexOf() like this:

function checkArray(str, arr){
   for(var i=0; i < arr.length; i++){
       if(str.indexOf(arr[i]) > -1)
           return true;
   }
   return false;
}

checkArray(itemDetails,trackingList); // returns false

However, for strings with multiple words in them, such as 'alter figure', I want checkArray() to return true as long as both of the words in the string appear anywhere in itemDetails. So checkArray() should return true in the example above since both 'alter' and 'figure' are in itemDetails.

The method I'm currently using is to split each element in trackingList:

function splitTrackList(arr){
    var newarr = [];
    for(var i = 0; i < arr.length; i++){
        newarr[i] = arr[i].split(" ");
    }
    return newarr;
}

trackList = splitTrackList(trackList); 
// [['alter', 'figure'], ['magic'], ['sword', 'art']]

And then compare using indexOf():

function newCheckArray(str, arr){
    var passed = true;
    for(var i=0; i < arr.length; i++){
        for(var j=0; j < arr[i].length; j++){
            if(str.indexOf(arr[i][j]) == -1){
                passed = false;
                break;
            }
            else passed = true;
        }

        if(passed) //stop loop if match found
            break;
    }
    return passed;
}

newCheckArray(itemDetails,trackingList); //returns true

My method works so far but I'm guessing there is a much faster/efficient way of doing this. Please let me know what you think. Thank you in advance.

Upvotes: 2

Views: 4125

Answers (4)

David Ibl
David Ibl

Reputation: 911

Here is a solution with lambda expressions:

var itemDetails = '[alter] saber 1/8 figure unopened';
var trackingList = ['alter saber', 'magic', 'sword art'];
var trackingList2 = ['alter was', 'magic', 'sword art'];

if(trackingList.map(str => str.split(" ")).filter(arrtemp => arrtemp.filter(strin => itemDetails.indexOf(strin) > -1).length == arrtemp.length).length > 0) {
    console.debug("true");
} else {
    console.debug("false")
}

if(trackingList2.map(str => str.split(" ")).filter(arrtemp => arrtemp.filter(strin => itemDetails.indexOf(strin) > -1).length == arrtemp.length).length > 0) {
    console.debug("true");
} else {
    console.debug("false")
}

https://jsfiddle.net/wrz1m0b5/

The regex solution only works if words are in correct order.

Upvotes: 0

denov
denov

Reputation: 12688

I would do something like

https://jsfiddle.net/denov/FXjXq/3/

var arr = [
    "cat dog mouse",
    "blue yellow green",
    "pizza burrito hamburger"
];

function isInString(needle, haystack) {
    return new RegExp('\\b' + needle + '\\b').test(haystack);
}

function checkForStringInArray(stringToFind, arr) {
    var inString = false;
    for(var i=0; i < arr.length; i++){ 
        inString = isInString(stringToFind, arr[i]);
        if(inString) break;
    }
    return inString;
}

var inString = checkForStringInArray('yellow', arr);

Upvotes: 2

Yabada
Yabada

Reputation: 1758

This should work :

function checkArray(str, arr){
      for (var j = 0; j < arr.length;j++) {
          wordtab = splitTrackList(arr[j]);
          for(var i=0; i < tab.length; i++){
               if(str.indexOf(wordtab[i]) > -1)
                   return true;
           }
       }
    return false;
}

Upvotes: 1

Alexander Traikov
Alexander Traikov

Reputation: 76

You can use regular expressions. I am giving you an example, although it is not the most efficient

function checkArray(str, arr){
   for(var i=0; i < arr.length; i++){
       if(str.match((".*" + arr[i].trim() + ".*").replace(" ", ".*")))
           return true;
   }
   return false;
}

Here I alter the string "key1 keyn" to ".*key1.keyn." so it could match those keys everywhere in the string. Have in mind that this will match them if they are part of another word. Good luck.

P.S. Learn regular expressions. They are pretty important in almost every language.

Upvotes: 1

Related Questions