title: How to "inner join" two arrays by a key in javascript?
body: <p>I have two arrays containing objects like so:</p>
  <pre><code>const array1 = 
  [
  {
      &quot;id&quot;: 1,
      &quot;mass&quot;: 149,
      &quot;height&quot;: 180,
      &quot;dob&quot;: &quot;2003-09-04&quot;
  },
  {
      &quot;id&quot;: 2,
      &quot;mass&quot;: 140,
      &quot;height&quot;: 175,
      &quot;dob&quot;: &quot;2000-02-12&quot;,
  },
  {
      &quot;id&quot;: 3,
      &quot;mass&quot;: 143,
      &quot;height&quot;: 170,
      &quot;dob&quot;: &quot;2001-11-04&quot; 
  }
  ]
  
  const array2 = 
  [
  {
      &quot;id&quot;: 1,
      &quot;name&quot;: &quot;James&quot;,
      &quot;sport&quot;: &quot;Football&quot;
  },
  {
      &quot;id&quot;: 2,
      &quot;name&quot;: &quot;Adam&quot;,
      &quot;sport&quot;: &quot;Tennis&quot;
  }
  ]
  </code></pre>
  <p>I would like to merge them such that a combined array only contains data where the <code>id</code> appears in both array1 and array2.</p>
  <p>So far I'm using lodash <code>_.merge</code> which returns:</p>
  <pre><code>const merged = _.merge(array1, array2)
  
  merged = [
  {
  &quot;id&quot;: 1,
  &quot;mass&quot;: 149,
  &quot;height&quot;: 180,
  &quot;dob&quot;: &quot;2003-09-04&quot;,
  &quot;name&quot;: &quot;James&quot;,
  &quot;sport&quot;: &quot;Football&quot;
  },
  {
  &quot;id&quot;: 2,
  &quot;mass&quot;: 140,
  &quot;height&quot;: 175,
  &quot;dob&quot;: &quot;2000-02-12&quot;,
  &quot;name&quot;: &quot;Adam&quot;,
  &quot;sport&quot;: &quot;Tennis&quot;
  },
  {
  &quot;id&quot;: 3,
  &quot;mass&quot;: 143,
  &quot;height&quot;: 170,
  &quot;dob&quot;: &quot;2001-11-04&quot; ,
  }
  ]
  </code></pre>
  <p>I would just like <code>merged</code> to contain data where <code>id</code> appears in both, i.e:</p>
  <pre><code>[{
  &quot;id&quot;: 1,
  &quot;mass&quot;: 149,
  &quot;height&quot;: 180,
  &quot;dob&quot;: &quot;2003-09-04&quot;,
  &quot;name&quot;: &quot;James&quot;,
  &quot;sport&quot;: &quot;Football&quot;
  },
  {
  &quot;id&quot;: 2,
  &quot;mass&quot;: 140,
  &quot;height&quot;: 175,
  &quot;dob&quot;: &quot;2000-02-12&quot;,
  &quot;name&quot;: &quot;Adam&quot;,
  &quot;sport&quot;: &quot;Tennis&quot;
  }]
  </code></pre>
  <p>I believe the equivalent function in something like SQL would be an &quot;Inner join&quot;</p>
  <p>Any help would be greatly appreciated!</p>