Group json data by a given object

Asked

Viewed 2,748 times

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 I care, I wanted to group these, the rest is non-standard variant that I do not care.

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

2 answers

4


I created an example that groups the array elements, by the property category of each item, creating an object in which 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; }

  • I liked your method for printing JSON in the DOM html, is public domain? I’ve used it in my answer. Hehe. And +1 for the answer.

  • @Fernando Como like this "public domain"... i used only browser features. Feel free to use where you want. =)

  • Yes, yes! I know, I was just referring to having used form copy paste. But seriously now, the third parameter of JSON.stringify, did not know, very smart the resource.

  • 1

    I didn’t know either, I found out by producing this answer... by looking for a way to make JSON cute. Then I found out that the browser is already able to do this... cool right!

2

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

Implementation would thus be:

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.

Advantages of the library

You have many more features available besides the group, as we can see here in the demo and in the library test file.

  • Is this like . net’s Dream? Very crazy your answer.

  • To be, to be is not, but I believe that the author’s idea was this. Hehe. But it is very nice to use and similar to Linq’s . net, helps a lot, and works perfect and fast, for what it proposes. It’s just a little abandoned by the authors who since 2011 haven’t had but no commit on github. (I myself have already added some features, in my local version, maybe in the future commito on github)

  • Thanks for the explanation, I will certainly try to know a little more.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.