Javascript数组对嵌套对象进行分组和求和

问题描述 投票:0回答:1

我需要根据一系列游戏为这些玩家创建一个记分板。每场比赛都有一个具有多个p[层的最终得分。每个球员都有一个代表他们所效力球队的属性。该属性需要用于总结每场比赛的得分。

数据如下:

var games = [
  {
      'id': '1',
       "teamOneScore": 10,
       "teamTwoScore": 5,
      'players': [

{
                        "username": "waters",
                        "displayName": "Waters Adkins",
                        "gender": "M",
                        "image": null,
                        "team": 2,
                        "appUserId": "86ba61dd-df18-4c42-b788-1d859507bab1"
                    },
                    {
                        "username": "whitney",
                        "displayName": "Whitney Winters",
                        "gender": "F",
                        "image": null,
                        "team": 1,
                        "appUserId": "c22bfafa-337a-4cbd-840d-e60adeb23625"
                    }
          
      ]
  }, {
      'id': '4',
       "teamOneScore": 25,
       "teamTwoScore": 10,
      'players': [

{
                        "username": "waters",
                        "displayName": "Waters Adkins",
                        "gender": "M",
                        "image": null,
                        "team": 2,
                        "appUserId": "86ba61dd-df18-4c42-b788-1d859507bab1"
                    },
                    {
                        "username": "whitney",
                        "displayName": "Whitney Winters",
                        "gender": "F",
                        "image": null,
                        "team": 1,
                        "appUserId": "c22bfafa-337a-4cbd-840d-e60adeb23625"
                    }
          
      ]
  }, {
      'id': '6',
       "teamOneScore": 10,
       "teamTwoScore": 5,
      'players': [

{
                        "username": "waters",
                        "displayName": "Waters Adkins",
                        "gender": "M",
                        "image": null,
                        "team": 2,
                        "appUserId": "86ba61dd-df18-4c42-b788-1d859507bab1"
                    },
                    {
                        "username": "whitney",
                        "displayName": "Whitney Winters",
                        "gender": "F",
                        "image": null,
                        "team": 1,
                        "appUserId": "c22bfafa-337a-4cbd-840d-e60adeb23625"
                    }
          
      ]
     
  }
];

结果应该是添加了总得分属性的玩家数组。

 'players': [

{
                        "username": "waters",
                        "displayName": "Waters Adkins",
                        "gender": "M",
                        "totalScore": 20,
                        "appUserId": "86ba61dd-df18-4c42-b788-1d859507bab1",

                    },
                    {
                        "username": "whitney",
                        "displayName": "Whitney Winters",
                        "gender": "F",
                        "totalScore": 45,
                        "appUserId": "c22bfafa-337a-4cbd-840d-e60adeb23625"
                    }
          
      ]```



 
javascript arrays mapping reduce
1个回答
0
投票
  1. players
    创建索引对象。键是
    appUserId
    ,值是
    players
    index

  2. 遍历每个游戏的

    players
    ,判断是现有用户还是新用户。

  3. 如果是新用户,请为他们在该游戏中的团队提供适当的分数并将其添加到

    palyers
    。对于现有玩家,将他们的分数添加到
    totalScore

const playersIdx = {}; // variables for indexing
const players = []; // result
games.forEach((game) =>
  game.players.forEach((player) => {
    const findPlayer = playersIdx[player.appUserId] ?? -1;
    const point =  player.team === 1 ? game.teamOneScore : game.teamTwoScore;
    if (findPlayer > -1) {
      players[findPlayer].totalScore += point
    } else {
      const newPlayer = { ...player, totalScore: point }
      delete newPlayer.team
      players.push(newPlayer);
      playersIdx[player.appUserId] = players.length - 1;
    }
  })
);
© www.soinside.com 2019 - 2024. All rights reserved.