简体   繁体   中英

Check if a javascript array contains all elements or part of element values of another array

I'm looking for a solution to the following problem:

var haystack_1 = ['banana', 'apple', 'orange'];
var haystack_2 = ['banana', 'apple', 'lemon'];
var haystack_3 = ['banana', 'apple', 'bloodorange'];
var needle_1 = ['apple', 'banana', 'or'];

Result when matching should be:

//needle_1 haystack_1 -> true
//needle_1 haystack_2 -> false
//needle_1 haystack_3 -> true

Already working for whole words:

function arrayContainsAnotherArray(needle, haystack){
  for(var i = 0; i < needle.length; i++){ 
    if(haystack.indexOf(needle[i]) === -1)
       return false;
  }
  return true;
}

This works perfectly for exact matches like:

var needle_2 = ['apple', 'banana', 'bloodorange'];
return arrayContainsAnotherArray(needle_2 , haystack_1); //false
return arrayContainsAnotherArray(needle_2 , haystack_2); //false
return arrayContainsAnotherArray(needle_2 , haystack_3); //true

How can I combine this function with matching parts of words, also?

just a slight function modification....

function arrayContainsAnotherArray(needle, haystack){
  for(var i = 0; i < needle.length; i++){ 
    if(!haystack.some(hay => hay.indexOf(needle[i]) > -1))
       return false;
  }
  return true;
}

just instead checks if "there is not some item in the haystack that contains the neddle, return false"

note that this is case sensitive.

You can use Array#every() and Array#some() along with String#includes() for partial matches

 var haystack_1 = ['banana', 'apple', 'orange']; var haystack_2 = ['banana', 'apple', 'lemon']; var haystack_3 = ['banana', 'apple', 'bloodorange']; function arrayContainsAnotherArray(needle, haystack){ return haystack.every(s=> needle.some(v => s.includes(v))) } var needle_2 = ['apple', 'banana', 'bloodorange']; console.log(arrayContainsAnotherArray(needle_2 , haystack_1)); //false console.log(arrayContainsAnotherArray(needle_2 , haystack_2)); //false console.log(arrayContainsAnotherArray(needle_2 , haystack_3)); //true 

You could check the needles agains the haystack with includes .

 function check(needle, haystack) { return haystack.every(h => needle.some(n => h.includes(n))); } var haystack_1 = ['banana', 'apple', 'orange']; var haystack_2 = ['banana', 'apple', 'lemon']; var haystack_3 = ['banana', 'apple', 'bloodorange']; var needle_1 = ['apple', 'banana', 'or']; console.log(check(needle_1, haystack_1)); // true console.log(check(needle_1, haystack_2)); // false console.log(check(needle_1, haystack_3)); // true 

You can use every with includes for checking all the parts:

 function arrayContainsAnotherArray(needle, haystack) { return needle.every(el => !!haystack.find(item => item.includes(el))); } var haystack_1 = ['banana', 'apple', 'orange']; var haystack_2 = ['banana', 'apple', 'lemon']; var haystack_3 = ['banana', 'apple', 'bloodorange']; var needle_1 = ['apple', 'banana', 'or']; console.log(arrayContainsAnotherArray(needle_1, haystack_1)); console.log(arrayContainsAnotherArray(needle_1, haystack_2)); console.log(arrayContainsAnotherArray(needle_1, haystack_3)); 

This works matching with regex instead of string equality, but it matches the needle anywhere in the haystack, so it returns true for your second example because apple and banana are found.

function needle_matches(haystack, needles) {
  for(var i = 0; i < needles.length; i++){
    if(haystack.some(function(e) { e.match(needles[i]) })) {
      return true;
    }
  }
  return false;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM