I would like a tip / help.
I'm working with a series of JSON
files, and applying filters to them as the user progresses through the system.
So far so good, I managed to do it quietly using .filter()
.
My problem is in the next step to this one. I need to apply the filter in this file, and with the filter result, summarize the information based on some columns only.
var as=$(ards).filter(function (i,n){
return n.ARMARIO_ERB===marker.getTitle();
});
OK, the filter is applied, configures the marker I need, at this point I 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 for simplicity, 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
Searching, I saw that it can be done through .reduce()
, I found some examples, but I could not apply any, I'm a layman.
I found this link: sum-and-group-by-of-json-data , it applies over a column just, how would I do for 4 for example, adding my columns?
Link example:
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.