平面 CSV 文件到嵌套 Javascript 对象

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

我有一个 CSV 文件,如下:

人员ID 姓名 游戏 日期 分数
123 约翰 网球 2023年1月1日 150
131 阿曼达 曲棍球 2023年1月2日 134
123 约翰 网球 2023年1月2日 140
134 彼得 网球 2023年2月2日 127

我需要采取什么步骤才能得到这个:

Tennis:

  John:

     Total_score: 290
     Played_games:
         01/01/2023   150
         01/02/2023   140
  Peter:

     Total_score: 127
     Played games:
         02/02/2023   127
Hockey:

  Amanda:

     Total_score: 134
     Played_games:
         01/02/2023   134

这样做的目的是制作一个按游戏>人物分组的记分板。 我们想要总结所玩游戏的得分。

我已经尝试过不同的事情。我得到了没有重复的游戏列表等等。但是制作对象的最佳实践是什么?

我阅读了有关映射、过滤等的内容,但找不到适合我的解决方案。也许我想得太复杂了。

目前我有以下 Javascript 数组:

const scores = [
      {"PersonID": "1234","Date": "31/12/2024", "game": "tennis", "name":"John", "score":151},
      {"PersonID": "1234","Date": "25/12/2024", "game": "tennis", "name":"John", "score":178},
      {"PersonID": "55513","Date": "01/12/2024", "game": "Hockey", "name":"Piet", "score":166},
      {"PersonID": "24244","Date": "10/11/2024", "game": "tennis", "name":"Klaas", "score":188},
      {"PersonID": "1234","Date": "06/12/2024", "game": "Football", "name":"John", "score":133},
      {"PersonID": "33245","Date": "24/12/2024", "game": "Baseball", "name":"Marie", "score":121},
      {"PersonID": "63542","Date": "11/12/2024", "game": "Kitesurfing", "name":"Peter", "score":185},
    ]

这就是我过滤游戏类型的方式

    var arrGames = [];
    scores.filter(function(item){
      var i = arrGames.findIndex(x => x.game== item.game);
      if(i <= -1){
        arrGames.push({game: item.game});
      }
      return null;
    });
    
    console.log(JSON.stringify(arrGames, null, 2));

下一步是我需要将人员作为子项添加到游戏中。我该如何安排这个?我知道这可以通过一些高级循环来完成。但对于我的学习过程来说,循序渐进是最好的:)

javascript arrays csv object nested
1个回答
0
投票

请尝试以下操作。

const scores = [
  {"PersonID": "1234","Date": "31/12/2024", "game": "tennis", "name":"John", "score":151},
  {"PersonID": "1234","Date": "25/12/2024", "game": "tennis", "name":"John", "score":178},
  {"PersonID": "55513","Date": "01/12/2024", "game": "Hockey", "name":"Piet", "score":166},
  {"PersonID": "24244","Date": "10/11/2024", "game": "tennis", "name":"Klaas", "score":188},
  {"PersonID": "1234","Date": "06/12/2024", "game": "Football", "name":"John", "score":133},
  {"PersonID": "33245","Date": "24/12/2024", "game": "Baseball", "name":"Marie", "score":121},
  {"PersonID": "63542","Date": "11/12/2024", "game": "Kitesurfing", "name":"Peter", "score":185},
];

var end = {};

scores.forEach(function(a) {
    let game = a.game;
    let player = a.name;
    let dt = a.Date;
    let score = a.score;

    if(!end.hasOwnProperty(game)) {
        end[game] = {};
    }

    if(!end[game].hasOwnProperty(player)) {
        end[game][player] = {
            "Total_score" : 0,
            "Played_games" : []
        };
    }

    end[game][player]["Total_score"] += score;
    end[game][player]["Played_games"].push({[dt]:score});
});

console.log(JSON.stringify(end));


说明

通过循环遍历您的

scores
数组,我们正在挑选您最终结果所需的内容。

第一个测试,

if(!end.hasOwnProperty(game)) {...}
只是将当前游戏指定为
end
对象中的键(如果它尚不存在)。例如,在我们的第一次运行中,
end
为空,而我们从您的
scores
数组中选取的第一个游戏是
tennis

接下来,我们检查特定游戏(第一次运行中的网球)中是否有我们当前正在招募的球员。如果玩家不在我们的特定游戏中,我们会将他们放在那里,并给他们零分和空的日期数组。

最后,我们用在当前

forEach
运行中选择的递增分数和日期/分数值来填充键。

这给了我们:

{
    "tennis": {
        "John": {
            "Total_score": 329,
            "Played_games": [
                {
                    "31/12/2024": 151
                },
                {
                    "25/12/2024": 178
                }
            ]
        },
        "Klaas": {
            "Total_score": 188,
            "Played_games": [
                {
                    "10/11/2024": 188
                }
            ]
        }
    },
    "Hockey": {
        "Piet": {
            "Total_score": 166,
            "Played_games": [
                {
                    "01/12/2024": 166
                }
            ]
        }
    },
    "Football": {
        "John": {
            "Total_score": 133,
            "Played_games": [
                {
                    "06/12/2024": 133
                }
            ]
        }
    },
    "Baseball": {
        "Marie": {
            "Total_score": 121,
            "Played_games": [
                {
                    "24/12/2024": 121
                }
            ]
        }
    },
    "Kitesurfing": {
        "Peter": {
            "Total_score": 185,
            "Played_games": [
                {
                    "11/12/2024": 185
                }
            ]
        }
    }
}

顺便说一句,阿曼达在示例 JSON 数组中的任何地方都没有提及,即使她出现在问题开头的表格中 - 这就是为什么她没有出现在

end
结果中。

请注意,此解决方案不涵盖玩家在特定日期玩了多场比赛的情况。如果您也需要,请在评论中告诉我。

© www.soinside.com 2019 - 2024. All rights reserved.