title: Merge arrays with underscore.js
body: <p>After performing a few underscore.js operations (<code>_.map</code>, <code>_.each</code>, <code>_.pluck</code>, and <code>_.flatten</code>), I have an array of objects that looks like this:</p>
  
  <pre><code>var userArray = [
    {id: 123456, username: "bill123", group: "ONE"},
    {id: 123457, username: "joe123", group: "TWO"},
    {id: 123457, username: "joe123", group: "TWO"},
    {id: 123458, username: "jim123", group: "ONE"}
  ]
  </code></pre>
  
  <p>I need to create a new array, with duplicates removed, and a count of how many times the object appeared in the array.  I am able to get the desired results with two separate <a href="http://underscorejs.org/" rel="nofollow">underscore.js</a> functions,  but am having trouble merging the two results.</p>
  
  <p>The working functions are as follows:</p>
  
  <pre><code>var uniqUsers = _.uniq(taggedUsers, false, function(user) {
    return user.id
  });
    //returns array of unique objects in the same format as above
    //[{id: 123457, username: "joe123", group: "TWO"},...]
  
  var userCounts = _.countBy(taggedUsers, "id");
    //returns the count for each user in the userArray in a single object
    //{123456: 1, 123457: 2, 123458: 1}
  </code></pre>
  
  <p><br></p>
  
  <p>What's the best approach for returning an array of objects like this:</p>
  
  <pre><code>{id: 123457, username: "joe123", group: "TWO", count: 2}
  </code></pre>
  
  <p>Can I add additional fields to the <code>_.countBy</code> function?  Or do I need to do something with <code>_.map</code>? </p>
  
  <p>Any help would be greatly appreciated ! Thanks</p>