title: d3 Grouping and Summarization
body: <p>I have a JSON data object consisting of zones, zip codes, and quantities of items for each zip code. I am trying to summarize (rollup) a summation of quantities and count of the number of records by zone. I am using d3.js currently, but am open to other libraries if they suit the need better.</p>
  
  <p>An example of the original data structure: </p>
  
  <pre><code>var test = [  
     {  
        "Name":"One Name",
        "Zip":"75001",
        "Zone":"A",
        "qty":60
     },
     {  
        "Name":"Two Name",
        "Zip":"75003",
        "Zone":"A",
        "qty":40
     },
     {  
        "Name":"Three Name",
        "Zip":"75009",
        "Zone":"B",
        "qty":20
     }
  ]
  </code></pre>
  
  <p>And here is the desired result I am trying to achieve:</p>
  
  <pre><code>[  
     {  
        "Zone":"A",
        "qtySum":100,
        "cnt":2,
        "values":[  
           {  
              "Name":"One Name",
              "Zip":"75001",
              "Zone":"A",
              "qty":60
           },
           {  
              "Name":"Two Name",
              "Zip":"75003",
              "Zone":"A",
              "qty":40
           }
        ]
     },
     {  
        "Zone":"B",
        "qtySum":20,
        "cnt":1,
        "values":[  
           {  
              "Name":"Three Name",
              "Zip":"75009",
              "Zone":"B",
              "qty":20
           }
        ]
     }
  ]
  </code></pre>
  
  <p>I can achieve the grouping without the summary data by doing:</p>
  
  <pre><code>d3.nest()
  .key(function(d) { return d.Zone; })
  .entries(test)
  </code></pre>
  
  <p>And I can rollup the data into a summary object by:</p>
  
  <pre><code>d3.nest()
  .key(function(d) { return d.Zone; })
  .rollup(function(v) { return {
          count: v.length,
          total: d3.sum(v, function(d) { return d.qty; }),
      }; 
  })
  .entries(test);
  </code></pre>
  
  <p>But I cannot manage to get both to work together. I thought about manually looping through the the summary object and stuffing it with the corresponding records from the original object, but that seems like it would be too much overhead.</p>