title: Group and summarize json data to new json object
body: <p>I am using angularJS and have a JSON object from an API response which looks like the following:</p>
  
  <pre><code>var data = [
    {"group": "red", "state": "running"},
    {"group": "red", "state": "closed"},
    {"group": "red", "state": "closed"},
    {"group": "blue", "state": "running"},
    {"group": "blue", "state": "running"}
  ];
  </code></pre>
  
  <p>I would like to parse this object inside a javascript function to get the following result:</p>
  
  <pre><code>var sumdata = [
    {"group": "red", "running": 1, "closed": 2, "summary": 3},
    {"group": "blue", "running": 2, "closed": 0, "summary": 2}
  ];
  </code></pre>
  
  <p>So, I have to group the first property called "group", then count how many objects in this group are in running state, closed state and also summarize the count of objects.</p>
  
  <p>(Note:
  I would not like to use extra javascript libraries like <a href="http://linqjs.codeplex.com/" rel="nofollow">LINQ.js</a>
  )</p>
  
  <p>Could yo help me please?</p>
  
  <p>I tried the following, which is missing the group by and have no idea how to put that into this function:</p>
  
  <pre><code>var getSum = function (data) {
  
      if (!data) {
  
          $scope.data = [];
      }
      else {
  
          for (var i = 0; i &lt; data.length; i++) {
  
              var group = data[i][0];
              var status = data[i][1];
  
              status = (status ? status.Name : "").toUpperCase();
  
              var running = 0;
              var closed = 0;
  
              switch (status) {
  
                  case "RUNNING":
                      running++;
                      break;
  
                  case "CLOSED":
                      closed++;
                      break;
  
                  default:
                      break;
              }
              var summary = running + closed;        
              $scope.dataSum.push({ "group": group, "running": running, "closed": closed, "summary": summary});
          }
      }
  
  };
  </code></pre>