3
I would like a tip/help.
I’m working with a number of files JSON
, and applying filters to them as the user advances in the system.
So far so good, I managed to do it quietly using the .filter()
.
My problem is in the next step to this. I need to apply the filter to this file, and with the filter result, summarize the information based on a few columns only.
var as=$(ards).filter(function (i,n){
return n.ARMARIO_ERB===marker.getTitle();
});
Okay, the filter is applied, comfort the marker I need, at this point print the filter information:
for (var i=0;i<as.length;i++){
conteudo_info += "<tr>" +
"<td>" + as[i].UF + "</td>" +
"<td>" + as[i].DESC_CLUSTER + "</td>" +
"<td>" + as[i].DESC + "</td>" +
"<td>" + as[i].QNTD1+ "</td>" +
"<td>" + as[i].QNTD2+ "</td>" +
"</tr>" ;
}
I put five columns just to simplify, I have around 10.
What I need to do is, instead of presenting the 10 columns, I would like to present the result in a consolidated way.
Example(Original Base):
PR CTBA A 1 1
PR CTBA B 8 5
PR MGA A 1 2
PR CTBA C 2 0
Consolidated:
PR CTBA 11 6
PR MGA 1 2
Researching, I saw that can be done through the .reduce()
, I found some examples, but I could not apply any, I am layman in the subject.
I found this link: sum-and-group-by-of-json-data, it applies on a column only, as I would for 4 for example, adding up my columns?
Example of the link:
var result = dataObject.reduce(function(res, obj) {
if (!(obj.category in res))
res.__array.push(res[obj.category] = obj);
else {
res[obj.category].hits += obj.hits;
res[obj.category].bytes += obj.bytes;
}
return res;
}, {__array:[]}).__array
.sort(function(a,b) { return b.bytes - a.bytes; });
Thank you.
To do these types of operations I use the jLinq library, see if it helps you! Not very good documentation, but it’s very useful.
– Fernando Leal
@Fernando took one look at this library.. It sounds interesting, but since only at this point will I use this consolidation, I’m trying to do it with . reduce().. Last I’ll try with jLink. Thank you.
– Fernando A.W.