我如何在对象数组中求和javascript数组的属性的总和?



我正在制作一款游戏,其中每个玩家都可以就给定的问题提交答案,其他玩家可以对另一个玩家的答案进行投票。我将这些结果存储在一个游戏回合数组中,该数组可能如下所示:

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()方法,我可以在round (roundHistory数组的索引)上使用。

我期望最终结果与我已经编写的函数的结果具有相同的形状:具有两个属性的对象数组:player(播放器对象)和totalVotes。e . g。

const result = [
{
"player": {
"id": "id1",
"name": "player1"
},
"totalVotes": 2
},
{
"player": {
"id": "id2",
"name": "player2"
},
"totalVotes": 1
},
{
"player": {
"id": "id3",
"name": "player3"
},
"totalVotes": 0
}
];

下面是实现预期目标的一种可能方法。

<<p>

代码片段/strong>

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 playern',
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 }

相关内容

  • 没有找到相关文章