[英]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.