title: Javascript groupby week/month/year
body: <p>I have a json array with data in it. i'd like to group the data in it by week, month &amp; year.</p>
  
  <p>I tried following the discussion <a href="https://stackoverflow.com/questions/16700678/javascript-object-group-by-day-month-year">Javascript groupby</a></p>
  
  <p>How can I get the sum of values grouped by month &amp; year as in the expected output show below please?</p>
  
  <p>Following the example quoted, I get the output as below</p>
  
  <p>Monthly Grouping's output</p>
  
  <p><a href="https://i.stack.imgur.com/AJNHA.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/AJNHA.jpg" alt="Monthly Grouping"></a></p>
  
  <p>Yearly Grouping's output</p>
  
  <p><a href="https://i.stack.imgur.com/KGPGa.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KGPGa.jpg" alt="Yearly Grouping"></a></p>
  
  <p>Here's the data &amp; code</p>
  
  <pre><code>function displayCalc() {
      var data= [{
              "EVENT_DATE": "2015-06-27",
              "NUMBER_EVENTS": 3,
              "FULL_EVENT_DURATION": 42.24,
              "FIRST_PAUSE": 42.24,
              "SECOND_PAUSE": 14.08,
              "THIRD_PAUSE": 42.24,
              "FOURTH_PAUSE": 42.24,
              "FIFTH_PAUSE": 42.24,
              "SIXTH_PAUSE": 42.24
          }, {
              "EVENT_DATE": "2015-06-28",
              "NUMBER_EVENTS": 3,
              "FULL_EVENT_DURATION": 27.54,
              "FIRST_PAUSE": 27.54,
              "SECOND_PAUSE": 9.18,
              "THIRD_PAUSE": 27.54,
              "FOURTH_PAUSE": 27.54,
              "FIFTH_PAUSE": 27.54,
              "SIXTH_PAUSE": 27.54
          },{
              "EVENT_DATE": "2015-07-02",
              "NUMBER_EVENTS": 39,
              "FULL_EVENT_DURATION": 354.39,
              "FIRST_PAUSE": 26.19,
              "SECOND_PAUSE": 9.08,
              "THIRD_PAUSE": 29.96,
              "FOURTH_PAUSE": 32.79,
              "FIFTH_PAUSE": 35,
              "SIXTH_PAUSE": 36.47
          }, {
              "EVENT_DATE": "2015-07-03",
              "NUMBER_EVENTS": 66,
              "FULL_EVENT_DURATION": 593,
              "FIRST_PAUSE": 26.26,
              "SECOND_PAUSE": 8.98,
              "THIRD_PAUSE": 29.21,
              "FOURTH_PAUSE": 31.24,
              "FIFTH_PAUSE": 33.21,
              "SIXTH_PAUSE": 34.56
          },{
              "EVENT_DATE": "2015-08-01",
              "NUMBER_EVENTS": 6,
              "FULL_EVENT_DURATION": 57.21,
              "FIRST_PAUSE": 28.6,
              "SECOND_PAUSE": 9.53,
              "THIRD_PAUSE": 35.33,
              "FOURTH_PAUSE": 36.17,
              "FIFTH_PAUSE": 36.68,
              "SIXTH_PAUSE": 37.02
          }, {
              "EVENT_DATE": "2015-08-02",
              "NUMBER_EVENTS": 12,
              "FULL_EVENT_DURATION": 94.47,
              "FIRST_PAUSE": 23.22,
              "SECOND_PAUSE": 7.87,
              "THIRD_PAUSE": 25.1,
              "FOURTH_PAUSE": 25.36,
              "FIFTH_PAUSE": 25.51,
              "SIXTH_PAUSE": 25.62
          }, {
              "EVENT_DATE": "2015-09-01",
              "NUMBER_EVENTS": 12,
              "FULL_EVENT_DURATION": 88.55,
              "FIRST_PAUSE": 6.84,
              "SECOND_PAUSE": 7.37,
              "THIRD_PAUSE": 9.18,
              "FOURTH_PAUSE": 9.82,
              "FIFTH_PAUSE": 10.21,
              "SIXTH_PAUSE": 10.48
          }, {
              "EVENT_DATE": "2015-09-03",
              "NUMBER_EVENTS": 15,
              "FULL_EVENT_DURATION": 116.13,
              "FIRST_PAUSE": 6.95,
              "SECOND_PAUSE": 7.74,
              "THIRD_PAUSE": 9.52,
              "FOURTH_PAUSE": 10.99,
              "FIFTH_PAUSE": 12.39,
              "SIXTH_PAUSE": 13.33
          }];
  
  
      var grouped = _.groupBy(data, function (item) {
          return item.EVENT_DATE;
      });
  
      var groupedByYear = _.groupBy(data, function (item) {
          return item.EVENT_DATE.substring(0, 4);
      });
  
      var groupedByMonth = _.groupBy(data, function (item) {
          return item.EVENT_DATE.substring(0, 7);
      });
  
      console.log(grouped);
      console.log(groupedByYear);
      console.log(groupedByMonth);
  }
  </code></pre>
  
  <p>Could I request help to achieve this please? </p>
  
  <p>The output is to be generated as below</p>
  
  <p><strong>WEEKLY AGGREGATED DATA</strong></p>
  
  <pre><code>     var jsonData = [{
      "EVENT_DATE": "2015-01-04",
      "NUMBER_EVENTS": 30,
      "FULL_EVENT_DURATION": 100.27,
      "FIRST_PAUSE": 142.24,
      "SECOND_PAUSE": 143.08,
      "THIRD_PAUSE": 305.24,
      "FOURTH_PAUSE":271.24,
      "FIFTH_PAUSE": 289.24,
      "SIXTH_PAUSE": 238.24
    }, {
      "EVENT_DATE": "2015-01-11",
      "NUMBER_EVENTS": 300,
      "FULL_EVENT_DURATION": 131.27,
      "FIRST_PAUSE": 124.24,
      "SECOND_PAUSE": 347.08,
      "THIRD_PAUSE": 245.24,
      "FOURTH_PAUSE":345.24,
      "FIFTH_PAUSE": 168.24,
      "SIXTH_PAUSE": 458.24
    }, {
      "EVENT_DATE": "2015-01-18",
      "NUMBER_EVENTS": 49,
      "FULL_EVENT_DURATION": 456.27,
      "FIRST_PAUSE": 23.24,
      "SECOND_PAUSE": 37.08,
      "THIRD_PAUSE": 678.24,
      "FOURTH_PAUSE":345.24,
      "FIFTH_PAUSE": 123.24,
      "SIXTH_PAUSE": 123.24
    }]
  </code></pre>
  
  <p><strong>MONTHLY AGGREGATED DATA</strong></p>
  
  <pre><code>   var jsonData = [{
      "EVENT_DATE": "2015-01",
      "NUMBER_EVENTS": 30,
      "FULL_EVENT_DURATION": 100.27,
      "FIRST_PAUSE": 142.24,
      "SECOND_PAUSE": 143.08,
      "THIRD_PAUSE": 305.24,
      "FOURTH_PAUSE":271.24,
      "FIFTH_PAUSE": 289.24,
      "SIXTH_PAUSE": 238.24
    }, {
      "EVENT_DATE": "2015-02",
      "NUMBER_EVENTS": 300,
      "FULL_EVENT_DURATION": 131.27,
      "FIRST_PAUSE": 124.24,
      "SECOND_PAUSE": 347.08,
      "THIRD_PAUSE": 245.24,
      "FOURTH_PAUSE":345.24,
      "FIFTH_PAUSE": 168.24,
      "SIXTH_PAUSE": 458.24
    }, {
      "EVENT_DATE": "2015-04",
      "NUMBER_EVENTS": 49,
      "FULL_EVENT_DURATION": 456.27,
      "FIRST_PAUSE": 23.24,
      "SECOND_PAUSE": 37.08,
      "THIRD_PAUSE": 678.24,
      "FOURTH_PAUSE":345.24,
      "FIFTH_PAUSE": 123.24,
      "SIXTH_PAUSE": 123.24
    }] 
  </code></pre>
  
  <p><strong>YEARLY AGGREGATED DATA</strong></p>
  
  <pre><code>       var jsonData = [{
      "EVENT_DATE": "2014",
      "NUMBER_EVENTS": 3000,
      "FULL_EVENT_DURATION": 1000.27,
      "FIRST_PAUSE": 1420.24,
      "SECOND_PAUSE": 1430.08,
      "THIRD_PAUSE": 3050.24,
      "FOURTH_PAUSE":2710.24,
      "FIFTH_PAUSE": 2890.24,
      "SIXTH_PAUSE": 2380.24
    }, {
      "EVENT_DATE": "2015",
      "NUMBER_EVENTS": 4000,
      "FULL_EVENT_DURATION": 131.27,
      "FIRST_PAUSE": 1240.24,
      "SECOND_PAUSE": 3470.08,
      "THIRD_PAUSE": 2450.24,
      "FOURTH_PAUSE":3450.24,
      "FIFTH_PAUSE": 1680.24,
      "SIXTH_PAUSE": 4580.24
    }]
  </code></pre>