title: How can I perform an inner join with two object arrays in JavaScript?
body: <p>I have two object arrays:</p>
  
  <pre><code>var a = [
    {id: 4, name: 'Greg'},
    {id: 1, name: 'David'},
    {id: 2, name: 'John'},
    {id: 3, name: 'Matt'},
  ]
  
  var b = [
    {id: 5, name: 'Mathew', position: '1'},
    {id: 6, name: 'Gracia', position: '2'},
    {id: 2, name: 'John', position: '2'},
    {id: 3, name: 'Matt', position: '2'},
  ]
  </code></pre>
  
  <p>I want to do an inner join for these two arrays <code>a</code> and <code>b</code>, and create a third array like this (if the position property is not present, then it becomes null):</p>
  
  <pre><code>var result = [{
    {id: 4, name: 'Greg', position: null},
    {id: 1, name: 'David', position: null},
    {id: 5, name: 'Mathew', position: '1'},
    {id: 6, name: 'Gracia', position: '2'},
    {id: 2, name: 'John', position: '2'},
    {id: 3, name: 'Matt', position: '2'},
  }]
  </code></pre>
  
  <p><strong>My approach:</strong></p>
  
  <pre><code>function innerJoinAB(a,b) {
      a.forEach(function(obj, index) {
          // Search through objects in first loop
          b.forEach(function(obj2,i2){
          // Find objects in 2nd loop
          // if obj1 is present in obj2 then push to result.
          });
      });
  }
  </code></pre>
  
  <p>But the time complexity is <code>O(N^2)</code>. How can I do it in <code>O(N)</code>? My friend told me that we can use reducers and <code>Object.assign</code>.</p>
  
  <p>I'm not able to figure this out. Please help.</p>