【发布时间】:2020-03-17 04:07:18
【问题描述】:
我有一个包含足球比赛数据的 JSON 文件
[
{
"game_id":"258716",
"game_date_start":"2016-08-15",
"season": "2016",
"team_1_id":"119",
"team_2_id":"120",
"team_1_goals_quantity":"2",
"team_2_goals_quantity":"1",
},
{
"game_id":"258717",
"game_date_start":"2016-09-15",
"season": "2016",
"team_1_id":"119",
"team_2_id":"122",
"team_1_goals_quantity":"1",
"team_2_goals_quantity":"1"
},
{
"game_id":"258718",
"game_date_start":"2016-07-15",
"season": "2016",
"team_1_id":"121",
"team_2_id":"119",
"team_1_goals_quantity":"1",
"team_2_goals_quantity":"1"
}
]
任务 #1。统计每支球队的总比赛场数
任务 #2。计算球队的总进球数(在所有比赛中)
我正在使用 Vue.js。我已经通过这种方式解决了任务 #1:
countGames() {
var data = this.$store.getters.games;
var arr1 = [];
var obj1 = {};
//Here I got all unique Team IDs
function getMatches() {
for (var i in data) {
obj1[data[i].team_1_id] = (obj1[data[i].team_1_id] || 0) + 1;
obj1[data[i].team_2_id] = (obj1[data[i].team_2_id] || 0) + 1
};
Object.keys(obj1).forEach(function(el, data) {
arr1.push( [ el, obj1[el]] );
});
};
getMatches();
var result = arr1.map(
([team_id, matches]) => ({team_id, matches}) //here I got { {team_id: "119", matches: 3}, {team_id: "120", matches: 1} ... }
);
return result;
}
它有效。但我不知道如何解决任务 #2 - 计算每支球队在所有比赛中的总进球数。问题是,有时团队的唯一 ID 可能位于 team_1_id,有时位于 team_2_id(如 JSON 示例中的团队“119”i)。
请帮我写这个脚本。
【问题讨论】:
-
game_id总是一样的?应该不一样吧? -
是的,每次都不一样,谢谢前注。
标签: javascript json vue.js