[英]Javascript -> Adding up values of nested objects with the same name
我正在尝试计算每个问题每个玩家的总得分。
对于每个问题,我都采用以下格式检索数据。
[
{
"_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
}
]
我希望基于此数据为每个玩家获得总分,但是我似乎找不到代码来累加每个玩家的积分。
结果是:彼得:14安德烈:12迈克尔:10伊比勒:8
关于如何实现这一目标的任何想法?
我试图用下面的代码得到要点:
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);
}
}
这至少告诉我console.log中每个玩家的得分。 但是现在添加它们以获得最终结果是我似乎无法弄清楚的事情。
您可以使用Map
进行计数,并使用数据构建一个数组。
var data = [{ 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 }], player: "Pieter", question: 1, __v: 0, _id: "5ab24e5e49e0f20a06d73ab7" }, { answers: [{ _id: "5ab227cf07818240935b11a5", player: "Peter", comment: "", points: 7, __v: 0 }, { _id: "5ab24bf3b8494c76fd8bb31a", player: "André", comment: "", points: 6, __v: 0 }, { _id: "5ab24bf7b8494c76fd2bb31b", player: "Maikel", comment: "", points: 5, __v: 0 }, { _id: "5ab24bfab8494c76fd9bb31c", player: "Iebele", comment: "", points: 4, __v: 0 }], player: "Kareltje", question: 1, __v: 0, _id: "5ab24e5e49e0f20b86d73ab7" }], score = Array.from( data.reduce((m, { answers }) => answers.reduce((n, { player, points }) => n.set(player, (n.get(player) || 0) + points), m), new Map), ([player, score]) => ({ player, score }) ); console.log(score);
.as-console-wrapper { max-height: 100% !important; top: 0; }
您可以使用此替代方法。
使用函数reduce
和forEach
函数。
reduce
积累。 forEach
遍历答案。 var data = [{ 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 } ], player: "Pieter", question: 1, __v: 0, _id: "5ab24e5e49e0f20a06d73ab7"},{ answers: [ { _id: "5ab227cf07818240935b11a5", player: "Peter", comment: "", points: 7, __v: 0 }, { _id: "5ab24bf3b8494c76fd8bb31a", player: "André", comment: "", points: 6, __v: 0 } , { _id: "5ab24bf7b8494c76fd2bb31b", player: "Maikel", comment: "", points: 5, __v: 0 } ,{ _id: "5ab24bfab8494c76fd9bb31c", player: "Iebele", comment: "", points: 4, __v: 0 } ], player: "Kareltje", question: 1, __v: 0, _id: "5ab24e5e49e0f20b86d73ab7"}]; var result = data.reduce((a, {answers}) => { answers.forEach(({player, points}) => a[player] = (a[player] || 0) + points); return a; }, {}); console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
您只需使用外部阵列即可。 它基本上对points
每个元素使用相同的键,并验证键是否已设置(如果未设置,则设置为零),并对与相同键相关的点求和。
PS: _id
在json中不是唯一的。
由于您提供的JSON无效,因此我使用以下代码:
var arr = [
{
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
}
],
player: "Pieter",
question: 1,
__v: 0,
_id: "5ab24e5e49e0f20a06d73ab7"
}, {
answers: [{
_id: "5ab227cf07818240935b11a5",
player: "Peter",
comment: "",
points: 7,
__v: 0
}, {
_id: "5ab24bf3b8494c76fd8bb31a",
player: "André",
comment: "",
points: 6,
__v: 0
}, {
_id: "5ab24bf7b8494c76fd2bb31b",
player: "Maikel",
comment: "",
points: 5,
__v: 0
}, {
_id: "5ab24bfab8494c76fd9bb31c",
player: "Iebele",
comment: "",
points: 4,
__v: 0
}
],
player: "Kareltje",
question: 1,
__v: 0,
_id: "5ab24e5e49e0f20b86d73ab7"
}
]
var points = {};
for (var i in arr) {
for (var j in arr[i].answers) {
if (!points[arr[i].answers[j].player]) {
points[arr[i].answers[j].player] = 0;
}
points[arr[i].answers[j].player] += arr[i].answers[j].points;
}
}
您可以使用.reduce
遍历数组并汇总所有分数:
const data = [{"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}],"player":"Pieter","question":1,"__v":0,"_id":"5ab24e5e49e0f20a06d73ab7"},{"answers":[{"_id":"5ab227cf07818240935b11a5","player":"Peter","comment":"","points":7,"__v":0},{"_id":"5ab24bf3b8494c76fd8bb31a","player":"André","comment":"","points":6,"__v":0},{"_id":"5ab24bf7b8494c76fd2bb31b","player":"Maikel","comment":"","points":5,"__v":0},{"_id":"5ab24bfab8494c76fd9bb31c","player":"Iebele","comment":"","points":4,"__v":0}],"player":"Kareltje","question":1,"__v":0,"_id":"5ab24e5e49e0f20b86d73ab7"}]; const scores = data.reduce( (scores, d) => { d.answers.forEach( answer => { scores[answer.player] = (scores[answer.player]||0) + answer.points; }); return scores; }, {}); console.log(scores);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.