title: Javascript group objects by property
body: <p>I'm trying to group objects in an array by date:</p>
  
  <pre><code>var list = [
       {
           date: "2017-01-01",
           type: "type1",
           amount: 100
       },
       {
           date: "2017-01-01",
           type: "type2",
           amount: 150
       },
       {
           date: "2017-01-02",
           type: "type1",
           amount: 200
       }
  ]
  </code></pre>
  
  <p>And I'm trying to get something like:</p>
  
  <pre><code>var dateArr = [
     {
        date: "2017-01-01",
        activities: [
          {
             type: "type1",
             amount: 100
          },
          {
             type: "type2",
             amount: 150
          }]
     }
  ]
  </code></pre>
  
  <p>I have tried a few things...like this using underscore (from here <a href="https://stackoverflow.com/a/15888912/4989305">https://stackoverflow.com/a/15888912/4989305</a>): </p>
  
  <pre><code>var dateArr = _
  .chain(list)
  .groupBy('date')
  .map(function(value, key) {
      return {
          date: key,
          activities: [{
              type: _.pluck(value, 'type'),
              amount: _.pluck(value, 'amount')
          }]
      }
  })
  .value();
  </code></pre>
  
  <p>I've also tried this (from here <a href="https://stackoverflow.com/a/31373860/4989305">https://stackoverflow.com/a/31373860/4989305</a>)</p>
  
  <pre><code>var dateArr = {};
  list.forEach(function(item){
      dateArr[item.date] = dateArr[item.date]||[];
      dateArr[item.date].push(item);
  });
  </code></pre>
  
  <p>But, for some reason both return empty.</p>
  
  <p>Any help will be greatly appreciated.</p>