title: Check whether elements from one array are present in another array
body: <p>What is the best way to check whether elements from one array are present in another array using JavaScript?</p>
  
  <p>I come up with two of the following methods (but neither of them do I like very much).</p>
  
  <p><strong>Method1</strong></p>
  
  <pre><code>for(let i = 0; i &lt; arr1.length; ++i) {
      for(let j = 0; j &lt; arr2.length; ++j) {
          if(arr1[i] === arr2[j]) {
              arr1[i].isPresentInArr2 = true;
              break;
          }
      }
  }
  </code></pre>
  
  <p><strong>Method2</strong></p>
  
  <pre><code>const idToObj = {};
  for(let i = 0; i &lt; arr2.length; ++i) {
      nameToObj[arr2[i].Id] = arr2[i];
  }
  for(let i = 0; i &lt; arr1.length; ++i) {
      if(nameToObj[arr1[i].Id]) {
          nameToObj[arr1[i].Id].isPresentInArr2 = true;
      }
  }
  </code></pre>
  
  <p>Here I am assuming that I have two arrays of objects: <code>arr1</code> and <code>arr2</code>. Those objects have a unique <code>Id</code> property each. And I am to check whether every object in <code>arr1</code> is present in <code>arr2</code>.</p>
  
  <p>I suppose the second method would be more efficient. Hope for interesting suggestions.</p>