0

Trying to run a simple check on an Array of Strings to see if it contains any elements from another Array of Strings but running into some unexpected behavior. In the tests below, both arrays have the b string.
However, the conditional statements do not seem to recognize it. Why is this happening?

    const array1 = [ 'a', 'b', 'c', 'd' ] ;
    const array2 = [ 'e' , 'f' , 'g' , 'b' ] ;

    // test 1
    array1.includes( ...array2 ) ? 
      console.log( 'error' ) :
      console.log( 'no error' ) ;
        
        
    // test 2
    array1.includes( 'e' , 'f' , 'g' , 'b' ) ?
      console.log( 'error' ) :
      console.log( 'no error' ) ;
        
        
    // test 3
    if( array1.includes( 'e' , 'f' , 'g' , 'b' ) ) { console.log( 'error' ) }
    else { console.log( 'no error' ) }
OshiniRB
  • 578
  • 1
  • 7
  • 21
  • 1
    P.s. "includes" checks whether an array includes a certain value among its entries (as per the MDN documentation). Your first array doesn't contain the second arrays within it, which is why it's not doing what you expected. In other words "includes" doesn't search for each individual item of the array you pass to it, it treats the second array as a single item, and searches to see if the whole of that second array is contained within one index of the first one – ADyson Oct 09 '21 at 17:30
  • 2
    `.includes()` takes one or two parameters: the value to search for, and optionally the starting index for the search. You cannot use it to search for several values. – Pointy Oct 09 '21 at 17:31
  • Thanks for the recommendation, I already read that post but I am trying to figure out why includes is not working as expected. Trying to understand what is going on here. – nothing special Oct 09 '21 at 17:31
  • 1
    See my "p.s." comment to understand why – ADyson Oct 09 '21 at 17:31
  • Ok, I get it now, includes() only checks for one value. I was missing that. So to check for multiple values you would use some() it seems. Is this right ? – nothing special Oct 09 '21 at 17:34
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes – ADyson Oct 09 '21 at 17:34
  • Yes you would use some(), as per the link I provided. – ADyson Oct 09 '21 at 17:34
  • Ok. Thanks for clearing that up. Much appreciated. – nothing special Oct 09 '21 at 17:37
  • array1.map(e=>array2.includes(e)).includes(true) – QuentinUK Oct 10 '21 at 08:26

2 Answers2

0

you can check if an array contains any element of another by using ES6 some

const found = arr1.some(r=> arr2.includes(r))

const array1 = [ 'a', 'b', 'c', 'd' ] ;
const array2 = [ 'e' , 'f' , 'g' , 'b' ] ;
const found = array1.some(r=> array2.includes(r));
console.log(found);
Gabriel
  • 670
  • 1
  • 8
  • 24
0

I think you may be misusing Array.prototype.includes. I think from the docs that it only is intended to check for the presence of a single element. Spreading values into it will result in unexpected behavior as JavaScript functions can take arbitrarily many arguments without causing an error but they may not be used.

To achieve the results you’re looking for, I would probably reach for aSet

// this would work
const overlap = test1.filter(t => test2.includes(t));

// this might be faster (would have to benchmark but theoretically)
const s1 = new Set(test1);
const s2 = new Set(test2);
const overlap2 = s1.filter(t => s2.has(t));

if(overlap.length > 0) {
  console.log(‘overlap!’);
}

if(overlap2.size > 0) {
  console.log(‘overlap!’);
}
Souperman
  • 5,057
  • 1
  • 14
  • 39