Group and Count With LinQ

Group data from an array and count to get a total value of products that are sold divided by groups

by Gabriel Correa

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/linq.js/2.2.0.2/linq.min.js"></script>

JavaScript

var dataArray = [ 
  { category: "botijão 13KG", hits: 5, bytes: 5100.31 },
  { category: "botijão 13KG", hits: 1, bytes: 5600.94 },
  { category: "botijão 13KG", hits: 1, bytes: 5900.10 },
  { category: "botijão 13KG", hits: 1, bytes: 5300.38 },
  { category: "botijão 13KG", hits: 1, bytes: 5000.24 },
  { category: "botijão 25KG", hits: 1, bytes: 1000.24 },
  { category: "botijão 25KG", hits: 1, bytes: 1000.24 },
  { category: "botijão 25KG", hits: 1, bytes: 1000.24 },
  { category: "botijão 25KG", hits: 1, bytes: 1000.24 } 
];

var aggregatedObject = Enumerable.From(dataArray)
        .GroupBy("$.category", null,
                 function (key, g) {
                     return {
                       category: key,
                       hits: g.Sum("$.hits"),
                       bytes: g.Sum("$.bytes")
                     }
        })
        .ToArray();

console.log(aggregatedObject);