Group json date for a given object

2

I have a list:

var dataString='[ 
 { "category" : "Search Engines", "hits" : 5, "bytes" : 50189 },
 { "category" : "Content Server", "hits" : 1, "bytes" : 17308 },
 { "category" : "Content Server", "hits" : 1, "bytes" : 47412 },
 { "category" : "Search Engines", "hits" : 1, "bytes" : 7601 },
 { "category" : "Business", "hits" : 1, "bytes" : 2847 },
 { "category" : "Content Server", "hits" : 1, "bytes" : 24210 },
 { "category" : "Internet Services", "hits" : 1, "bytes" : 3690 },
 { "category" : "Search Engines", "hits" : 6, "bytes" : 613036 },
 { "category" : "Search Engines", "hits" : 1, "bytes" : 2858 } 
]';

where category is a variant pattern that matters to me, I wanted to group these, the rest is non-standard variant that does not matter to me.

Group type an array of only Business in which I can go through all the business if I want and so it goes ..

    
asked by anonymous 02.04.2015 / 01:40

2 answers

3

I created an example that groups the elements of the array, by the category property of each item, creating an object where each key is a group containing the corresponding elements:

var dataString= '['+
 '{ "category" : "Search Engines", "hits" : 5, "bytes" : 50189 },'+
 '{ "category" : "Content Server", "hits" : 1, "bytes" : 17308 },'+
 '{ "category" : "Content Server", "hits" : 1, "bytes" : 47412 },'+
 '{ "category" : "Search Engines", "hits" : 1, "bytes" : 7601 },'+
 '{ "category" : "Business", "hits" : 1, "bytes" : 2847 },'+
 '{ "category" : "Content Server", "hits" : 1, "bytes" : 24210 },'+
 '{ "category" : "Internet Services", "hits" : 1, "bytes" : 3690 },'+
 '{ "category" : "Search Engines", "hits" : 6, "bytes" : 613036 },'+
 '{ "category" : "Search Engines", "hits" : 1, "bytes" : 2858 } '+
']';

var data = JSON.parse(dataString);

var groupedData = {};

for (var it = 0; it < data.length; it++) {
  var item = data[it];
  if (!groupedData[item.category])
    groupedData[item.category] = [];
  groupedData[item.category].push(item);
}

document.body.innerHTML = "";
document.body.appendChild(document.createTextNode(JSON.stringify(groupedData, null, 4)));
body { white-space: pre; font-family: monospace; }
    
02.04.2015 / 01:53
2

It is a solution that brings a final result identical to that of reply @MiguelAngelo , but using a library that I really like and use some time (which is apparently abandoned by the developer) jLinq.js .

The implementation would look like this:

var dataString= '['+
 '{ "category" : "Search Engines", "hits" : 5, "bytes" : 50189 },'+
 '{ "category" : "Content Server", "hits" : 1, "bytes" : 17308 },'+
 '{ "category" : "Content Server", "hits" : 1, "bytes" : 47412 },'+
 '{ "category" : "Search Engines", "hits" : 1, "bytes" : 7601 },'+
 '{ "category" : "Business", "hits" : 1, "bytes" : 2847 },'+
 '{ "category" : "Content Server", "hits" : 1, "bytes" : 24210 },'+
 '{ "category" : "Internet Services", "hits" : 1, "bytes" : 3690 },'+
 '{ "category" : "Search Engines", "hits" : 6, "bytes" : 613036 },'+
 '{ "category" : "Search Engines", "hits" : 1, "bytes" : 2858 } '+
']';

var data = JSON.parse(dataString);

var result = jlinq.from(data)
                  // para ser case sensitive
                  .useCase()
                  // aplica group pelo campo categoria
                  .group("category");

// para imprimir do DOM (copiado na cara dura da implementação do @MiguelAngelo)
document.body.innerHTML = "";
document.body.appendChild(document.createTextNode(JSON.stringify(result, null, 4)));
body { white-space: pre; font-family: monospace; }
<script src="http://hugoware.net/resources/projects/jlinq/jlinq.min.demo-version.js"></script>
  

Example also available here in jsFiddle.

Library benefits

You have many more features available besides the group, as we can see here in the demo and Library Test File .

    
02.04.2015 / 03:18