You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JavaScript:累加同名嵌套对象分值,统计玩家每题总得分

用JavaScript计算每位玩家的得分

嘿,我来帮你搞定这个得分计算的需求!先看看你给出的数据结构:每一个外层对象代表一道题,里面的answers数组记录了不同玩家在这道题上的得分。接下来我会分两种常见的需求场景给出实现代码,你可以根据实际情况调整。

场景1:计算每位玩家在每道题的得分,以及所有题的总得分

这个场景下,我们会整理出每个玩家在每道题上拿到的分数,同时算出他们的累计总分。

// 假设你的原始数据存在这个变量里
const questions = [
  {
    "_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":"5ab24e5e49e0f20a06d73ab8",
    "player":"Kareltje",
    "answers": [
      { "_id":"5ab227cf07818240934b11a6", "player":"Peter", "comment":"", "points":8, "__v":0 },
      { "_id":"5ab24bf3b8494c76fd0bb31c", "player":"André", "comment":"", "points":9, "__v":0 }
    ]
  }
];

// 初始化结果对象,用来存储每个玩家的得分信息
const playerScores = {};

questions.forEach(question => {
  const questionId = question._id;
  question.answers.forEach(answer => {
    const { player, points } = answer;
    // 如果玩家还没在结果里,先初始化结构
    if (!playerScores[player]) {
      playerScores[player] = {
        total: 0,
        perQuestion: {}
      };
    }
    // 记录该玩家在这道题的得分
    playerScores[player].perQuestion[questionId] = points;
    // 累加总分
    playerScores[player].total += points;
  });
});

console.log(playerScores);

运行这段代码后,你会得到类似这样的结果:

{
  Peter: {
    total: 15,
    perQuestion: {
      '5ab24e5e49e0f20a06d73ab7': 7,
      '5ab24e5e49e0f20a06d73ab8': 8
    }
  },
  André: {
    total: 15,
    perQuestion: {
      '5ab24e5e49e0f20a06d73ab7': 6,
      '5ab24e5e49e0f20a06d73ab8': 9
    }
  },
  Maikel: {
    total: 5,
    perQuestion: {
      '5ab24e5e49e0f20a06d73ab7': 5
    }
  }
}

场景2:计算每道题中,该题对应玩家的总得分(即所有评分的总和)

如果外层的player字段是这道题的提交者,你需要计算该提交者在这道题上获得的总评分(所有answers的points相加),可以用下面的代码:

const questionScores = questions.map(question => {
  const totalPoints = question.answers.reduce((sum, answer) => sum + answer.points, 0);
  return {
    questionId: question._id,
    player: question.player,
    totalScore: totalPoints,
    individualScores: question.answers.map(ans => ({ player: ans.player, points: ans.points }))
  };
});

console.log(questionScores);

运行后会得到每道题的得分汇总:

[
  {
    questionId: '5ab24e5e49e0f20a06d73ab7',
    player: 'Kareltje',
    totalScore: 18,
    individualScores: [
      { player: 'Peter', points: 7 },
      { player: 'André', points: 6 },
      { player: 'Maikel', points: 5 }
    ]
  },
  {
    questionId: '5ab24e5e49e0f20a06d73ab8',
    player: 'Kareltje',
    totalScore: 17,
    individualScores: [
      { player: 'Peter', points: 8 },
      { player: 'André', points: 9 }
    ]
  }
]

这两种场景应该能覆盖你的需求啦,如果还有特殊的计算逻辑,随时调整代码里的处理步骤就行~

内容的提问来源于stack exchange,提问作者Maikey

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:46:19