DEV Community

The moment I realized forEach() does not return anything.

Kay N. on August 31, 2019

Javascript ES5's forEach() method and I had been getting along quite well until last week when I was trying to implement it inside this one simple ...
Collapse
 
gautierlepire profile image
Gautier Le Pire •

Hi! You might be looking for a function named Array.some. It checks that at least one item in an array verifies a given predicate, and is early-terminating.

Here's the code using a hash table (so both arrays must contain only strings) as you did:

const haveCommonItems = (array1, array2) => {
    let hashTable = {}
    array1.forEach(item => hashTable[item] = true)
    return array2.some(item => hashTable[item])
}

:)

Collapse
 
kaynguyen profile image
Kay N. •

Yes that sure does the job <3! Thanks for recommending!

Collapse
 
tbroyer profile image
Thomas Broyer •

And you may want to use new Set(array1) instead of your hashTable.

const haveCommonItems = (array1, array2) => {
    const array1set = new Set(array1)
    return array2.some(array1set.has)
}

(disclaimer: untested code snippet)

Thread Thread
 
kaynguyen profile image
Kay N. •

Awesome! Here's my tested code:

const haveCommonItems = (array1, array2) => {
    const array1set = new Set(array1);
    return array2.some(item => array1set.has(item));

haveCommonItems(animals, pets); // returns "true"
}

Very clean and time optimized! Thanks again everyone!

Collapse
 
iaboelsuod profile image
Ibrahim Ibrahim •

I was thinking why foreach ALWAYS returns undefined by design. But in your use case it would have been better to use find & return whatever it returns. It can be chained and will break once the element is found.

Collapse
 
theosyslack_42 profile image
Theo Syslack •

You might want to consider using Array.prototype.some. This will run a function against each element in an array, until something returns truthy. Then, it immediately stops and returns true. This will help a lot if you have a large number of items in the second array.
developer.mozilla.org/en-US/docs/W...

Collapse
 
kaynguyen profile image
Kay N. •

Oh yeah totally forgot about that little guy. Thanks for mentioning! Will definitely use it next time similar needs come up!

Collapse
 
freebld profile image
Cristian •

Hi, sorry I know it's just a code comment but in the first step when you iterate through the first array and tag items to true, your last output is dog. Should it not be kangaroo? 🤔
Thanks for this article👍

Collapse
 
kaynguyen profile image
Kay N. •

Hey my bad - I totally forgot about the guy! (poor little kangaroo!). Already put it back in with the gang! Thanksss for pointing this out! <3

Collapse
 
clarity89 profile image
Alex K. •

I prefer for.. of loop since it's more intuitive and has shorter syntax than the usual for loop.

BTW, Array#forEach is ES5 not ES6 feature ;)

Collapse
 
kaynguyen profile image
Kay N. •

Definitely! Thanks for mentioning <3

Collapse
 
mavarnare profile image
Miguel Varona •

Hi! Hace you considered using the .includes() method? If not, can you tell me why? I'm kind of new to JS.

Thanks for your article!

Collapse
 
kaynguyen profile image
Kay N. • • Edited

Hi Mike yes you totally can use includes() however it would make the function not algorithm-wise because you gotta nest one loop inside another:

const haveCommonItems = (array1, array2) => {
  for (i = 0; i < array1.length; i++) {
    if (array2.includes(array1[i])) {
      return true;
    }
  }
};

haveCommonItems(animals, pets); // returns "true"

So you nest includes() which loops thru array2 inside forEach(), which is not ideal in terms of time complexity if you have a huge amount of items in your arrays.