我正在制作一個游戲,每個玩家都可以提交給定問題的答案,而其他玩家可以對另一個玩家的答案進行投票。我將這些結果存盤在一系列游戲回合中,這些回合可能如下所示:
const roundHistory = [
{
question: 'question1',
submissions: [
{
explanation: 'answer1',
player: { id: 'id1', name: 'player1' },
votes: [
{ id: 'id2', name: 'player2' },
{ id: 'id3', name: 'player3' },
],
},
{
explanation: 'answer2',
player: { id: 'id2', name: 'player2' },
votes: [{ id: 'id1', name: 'player1' }],
},
{
explanation: 'answer3',
player: { id: 'id3', name: 'player3' },
votes: [],
},
],
},
{
question: 'question2',
submissions: [
{
explanation: 'answer1',
player: { id: 'id1', name: 'player1' },
votes: [
{ id: 'id2', name: 'player2' },
{ id: 'id3', name: 'player3' },
],
},
{
explanation: 'answer2',
player: { id: 'id2', name: 'player2' },
votes: [{ id: 'id1', name: 'player1' }],
},
{
explanation: 'answer3',
player: { id: 'id3', name: 'player3' },
votes: [],
},
],
},
];
如您所見,游戲中有 3 名玩家,因為每輪(roundHistory 陣列的索引)有 3 次提交。每個提交都有一個屬性player,代表提交提交的玩家。然后每個提交都有一個屬性,該屬性votes是為該提交投票的玩家陣列。現在我的問題:
我如何才能獲得所有回合中每位玩家的總票數?
到目前為止我所嘗試的......我想,首先我會得到每輪每名玩家的總票數,如下所示:
const roundHistory = [{question:'question1',submissions:[{explanation:'answer1',player:{id:'id1',name:'player1'},votes:[{id:'id2',name:'player2'},{id:'id3',name:'player3'},],},{explanation:'answer2',player:{id:'id2',name:'player2'},votes:[{id:'id1',name:'player1'}],},{explanation:'answer3',player:{id:'id3',name:'player3'},votes:[],},],},{question:'question2',submissions:[{explanation:'answer1',player:{id:'id1',name:'player1'},votes:[{id:'id2',name:'player2'},{id:'id3',name:'player3'},],},{explanation:'answer2',player:{id:'id2',name:'player2'},votes:[{id:'id1',name:'player1'}],},{explanation:'answer3',player:{id:'id3',name:'player3'},votes:[],},],},];
const getTotalReceivedVotesPerPlayerInRound = (round) => {
return roundHistory[round].submissions.map((s) => ({
player: s.player,
totalVotes: s.votes.length,
}));
}
console.log(getTotalReceivedVotesPerPlayerInRound(0));
但是我被困在將所有回合的所有結果加在一起。請注意,我試圖簡化所有使用的物件,例如播放器。在我的應用程式中,這些主要是我有幾個實用方法的類。其中之一是getTotalReceivedVotesPerPlayerInRound()我可以在一輪中使用的方法(roundHistory 陣列的索引)。
我希望最終結果與我已經撰寫的函式的結果具有相同的形狀:具有兩個屬性的物件陣列:(player玩家物件)和totalVotes. 例如:
const result = [
{
"player": {
"id": "id1",
"name": "player1"
},
"totalVotes": 2
},
{
"player": {
"id": "id2",
"name": "player2"
},
"totalVotes": 1
},
{
"player": {
"id": "id3",
"name": "player3"
},
"totalVotes": 0
}
];
uj5u.com熱心網友回復:
下面介紹的是實作預期目標的一種可能方式。
代碼片段
const getPlayerTotalVotes = arr => (
Object.values(
arr.reduce(
(acc, { submissions}) => {
submissions.forEach(
({ player: { id, name }, votes }) => {
acc[id] ??= { player: { id, name }, totalVotes: 0 };
acc[id].totalVotes = votes.length;
}
)
return acc;
},
{}
)
)
);
/* explanation of the code
method to obtain the total votes per-player for all rounds
const getPlayerTotalVotes = arr => (
Object.values( // extract only the values of the below intermediate result object
arr.reduce( // ".reduce()" to iterate and sum votes-count
(acc, { submissions}) => { // "acc" is the accumulator; destructure iterator
// to directly access "submissions" array
// then, iterate using ".forEach()" over "submissions"
submissions.forEach(
// destructure to access "votes" array and the particular player "id"
({ player: { id }, votes }) => {
// conditionally-assign 0 if "id" not already present in "acc"
acc[id] ??= 0;
// count the "votes" and add it to the "acc" prop with key as "id"
acc[id] = votes.length;
}
)
return acc; // explicitly return "acc" for each iteration
},
{} // "acc" is initialized as an empty object
) // implicit return of the result from ".reduce()" to caller
)
);
*/
const roundHistory = [{
question: 'question1',
submissions: [{
explanation: 'answer1',
player: {
id: 'id1',
name: 'player1'
},
votes: [{
id: 'id2',
name: 'player2'
},
{
id: 'id3',
name: 'player3'
},
],
},
{
explanation: 'answer2',
player: {
id: 'id2',
name: 'player2'
},
votes: [{
id: 'id1',
name: 'player1'
}],
},
{
explanation: 'answer3',
player: {
id: 'id3',
name: 'player3'
},
votes: [],
},
],
},
{
question: 'question2',
submissions: [{
explanation: 'answer1',
player: {
id: 'id1',
name: 'player1'
},
votes: [{
id: 'id2',
name: 'player2'
},
{
id: 'id3',
name: 'player3'
},
],
},
{
explanation: 'answer2',
player: {
id: 'id2',
name: 'player2'
},
votes: [{
id: 'id1',
name: 'player1'
}],
},
{
explanation: 'answer3',
player: {
id: 'id3',
name: 'player3'
},
votes: [],
},
],
},
];
console.log(
'total votes per player\n',
getPlayerTotalVotes(roundHistory)
);
.as-console-wrapper { max-height: 100% !important; top: 0 }
解釋
添加到上述代碼段的行內注釋。
編輯 使用給定的方法獲得“每輪”票數
const roundHistory = [{question:'question1',submissions:[{explanation:'answer1',player:{id:'id1',name:'player1'},votes:[{id:'id2',name:'player2'},{id:'id3',name:'player3'},],},{explanation:'answer2',player:{id:'id2',name:'player2'},votes:[{id:'id1',name:'player1'}],},{explanation:'answer3',player:{id:'id3',name:'player3'},votes:[],},],},{question:'question2',submissions:[{explanation:'answer1',player:{id:'id1',name:'player1'},votes:[{id:'id2',name:'player2'},{id:'id3',name:'player3'},],},{explanation:'answer2',player:{id:'id2',name:'player2'},votes:[{id:'id1',name:'player1'}],},{explanation:'answer3',player:{id:'id3',name:'player3'},votes:[],},],},];
const getTotalReceivedVotesPerPlayerInRound = (round) => {
return roundHistory[round].submissions.map((s) => ({
player: s.player,
totalVotes: s.votes.length,
}));
}
const getTotalAllRounds = rh => (
Object.values(
rh.reduce(
(acc, _, idx) => {
getTotalReceivedVotesPerPlayerInRound(idx)?.forEach(
({player: { id, name}, totalVotes}) => {
acc[id] ??= { player: {id, name}, totalVotes: 0 };
acc[id].totalVotes = totalVotes;
}
);
return acc;
},
{}
)
)
);
console.log(getTotalAllRounds(roundHistory));
.as-console-wrapper { max-height: 100% !important; top: 0 }
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/497764.html
標籤:javascript 数组
