I'm trying to calculate the total point scored per player per question.
For each question, I'm retrieving the data in the following format.
[
{
"_id":"5ab24e5e49e0f20a06d73ab7",
"player":"Kareltje",
"answers":
[
{
"_id":"5ab227cf07818240934b11a5",
"player":"Peter",
"comment":"",
"points":7,
"__v":0
},
{
"_id":"5ab24bf3b8494c76fd0bb31a",
"player":"André",
"comment":"",
"points":6,
"__v":0
},
{
"_id":"5ab24bf7b8494c76fd0bb31b",
"player":"Maikel",
"comment":"",
"points":5,
"__v":0
},
{
"_id":"5ab24bfab8494c76fd0bb31c",
"player":"Iebele",
"comment":"",
"points":4,
"__v":0
},
{
"_id":"5ab24bfeb8494c76fd0bb31d",
"player":"Bettina",
"comment":"",
"points":3,
"__v":0
},
{
"_id":"5ab24c01b8494c76fd0bb31e",
"player":"Shirley",
"comment":"",
"points":2,
"__v":0
},
{
"_id":"5ab24c04b8494c76fd0bb31f",
"player":"Suzanne",
"comment":"",
"points":1,
"__v":0
}
],
"question":1,"__v":0
},
{
"_id":"5ab24fa21e7caa1132720e7a",
"player":"Maikel",
"answers":
[
{
"_id":"5ab24c04b8494c76fd0bb31f",
"player":"Suzanne",
"comment":"",
"points":7,
"__v":0
},
{
"_id":"5ab24bfab8494c76fd0bb31c",
"player":"Iebele",
"comment":"",
"points":6,
"__v":0
},
{
"_id":"5ab24bf3b8494c76fd0bb31a",
"player":"André",
"comment":"",
"points":5,
"__v":0
},
{
"_id":"5ab24c01b8494c76fd0bb31e",
"player":"Shirley",
"comment":"",
"points":4,
"__v":0
},
{
"_id":"5ab24bf7b8494c76fd0bb31b",
"player":"Maikel",
"comment":"",
"points":3,
"__v":0
},
{
"_id":"5ab24bfeb8494c76fd0bb31d",
"player":"Bettina",
"comment":"",
"points":2,
"__v":0
},
{
"_id":"5ab227cf07818240934b11a5",
"player":"Peter",
"comment":"",
"points":1,
"__v":0
}
],
"question":1,"__v":0
}
]
I'm want to have a total score for each player based on this data, but I can't seem to find a code, to add up the points per player.
The result be something like: Peter: 14 André: 12 Maikel: 10 Iebele: 8
Any ideas on how to achieve this?
I've tried to get the points with the following code:
var { data, players } = this.state;
var ArrLength = data.length;
console.log(data);
var j;
var x;
for (j = 0; j < ArrLength; j++) {
let answer = data[j].answers;
for (x = 0; x < answer.length; x++) {
console.log(answer[`${x}`].points);
}
}
This at least show me the points per player in the console.log. But now adding them to get an end result is something I can't seem to figure out.