I have a table with participant data that I would like to compare. If a participant has multiple result points in the table, I want a script to calculate the sum of all their results. This process should be repeated for each participant listed in the table. The table is generated from a database field which includes columns for Participant, Station, and Points:
aa Some1 1
dd Some1 2
aa sm2 3
dd sm2 4
bb sm3 5
ee sm3 6
For example, I need to create a new table with the summed results per participant:
aa - 4,
dd - 6,
bb - 5,
ee - 6
I've attempted to achieve this with the following code snippet:
$(document).ready(function () {
$("body").click(function () {
var rows = $("tbody tr");
var jo = [];
for (var i = 0; i < rows.length; i++) {
for (var j = 1; j <= rows.length; j++) {
var pnt1 = $(rows[i]).find(".pnt").html();
var stations1 = $(rows[i]).find(".station").html();
var res1 = $(rows[i]).find(".res").html();
if (pnt1 == $(rows[j]).find(".pnt").html()) {
res1 = parseInt(res1);
res2 = parseInt($(rows[j]).find(".res").html());
jo.push(pnt1, res1, res2);
break;
}
}
}
console.log(jo);
});
});
However, it seems I may be going about this the wrong way. Any assistance or guidance on solving this issue would be greatly appreciated.
Updated section based on comments:
<table id="pntsRes">
<thead>
<tr>
<th>Participants</th>
<th>Points</th>
</tr>
</thead>
<tbody>
<tr><td class="pnt">aa</td><td class="station">AES</td><td class="res">1</td></tr><tr><td class="pnt">dd</td><td class="station">AES</td><td class="res">2</td></tr>
<tr><td class="pnt">aa</td><td class="station">Science</td><td class="res">3</td></tr>
<tr><td class="pnt">dd</td><td class="station">Science</td><td class="res">4</td></tr><tr><td class="pnt">bb</td><td class="station">Airport</td><td class="res">5</td></tr>
<tr><td class="pnt">ee</td><td class="station">Airport</td><td class="res">6</td></tr></tbody>
</table>