title: How to use Lodash to merge two collections based on a key?
body: <p>I have two collections, and the objects have a common key "userId". As below:</p>
  
  <pre><code>var _= require('lodash');
  
  var a = [
    { userId:"p1", item:1},
    { userId:"p2", item:2},
    { userId:"p3", item:4}
  ];
  
  var b = [
    { userId:"p1", profile:1},
    { userId:"p2", profile:2}
  ];
  </code></pre>
  
  <p>I want to merge them based on "userId" to produce:</p>
  
  <pre><code>[ { userId: 'p1', item: 1, profile: 1 },
    { userId: 'p2', item: 2, profile:2 },
    { userId: 'p3', item: 4 } ]
  </code></pre>
  
  <p>I have these so far:</p>
  
  <pre><code>var u = _.uniq(_.union(a, b), false, _.property('userId'));
  </code></pre>
  
  <p>Which result in:</p>
  
  <pre><code>[ { userId: 'p1', item: 1 },
    { userId: 'p2', item: 2 },
    { userId: 'p3', item: 4 },
    { userId: 'p1', profile: 1 },
    { userId: 'p2', profile: 2 } ]
  </code></pre>
  
  <p>How can I merge them now?</p>
  
  <p>I tried _.keyBy but it results in:</p>
  
  <pre><code>{ p1: { userId: 'p1', profile: 1 },
    p2: { userId: 'p2', profile: 2 },
    p3: { userId: 'p3', item: 4 } }
  </code></pre>
  
  <p>which is wrong. </p>
  
  <p>What's the last step I should do?  </p>