所以我有一个包含板球比赛信息的数组。我想显示一个团队在一场比赛中得分的totalRun,还显示该totalRun与其他团队的对抗情况。
数组包含
module.exports.matchScore = [
{
"fk_matchID": 234017,
"fk_teamID": 198708,
"inning": 1,
"isDeclare": 0,
"isForfeited": 0,
"isFollowOn": 0,
"isAllOut": 0,
"totalRun": 404,
"totalWicket": 10
},
{
"fk_matchID": 234017,
"fk_teamID": 198752,
"inning": 2,
"isDeclare": 0,
"isForfeited": 0,
"isFollowOn": 0,
"isAllOut": 0,
"totalRun": 280,
"totalWicket": 10
},
{
"fk_matchID": 234017,
"fk_teamID": 198708,
"inning": 3,
"isDeclare": 1,
"isForfeited": 0,
"isFollowOn": 0,
"isAllOut": 0,
"totalRun": 81,
"totalWicket": 4
},
{
"fk_matchID": 234017,
"fk_teamID": 198752,
"inning": 4,
"isDeclare": 0,
"isForfeited": 0,
"isFollowOn": 0,
"isAllOut": 0,
"totalRun": 15,
"totalWicket": 0
},
let matchScore = require('./array');
let matchScoreData = matchScore.matchScore;
let forMatchDetail = matchScore.filter(function (matchScoreDetail) {
return (matchScoreDetail.fk_matchID === matchScoreDetail.fk_matchID) && (matchScoreDetail.fk_teamID === matchScoreDetail.fk_teamID);
});
console.log(forMatchDetail);
let againstMatchDetail = matchScore.filter(function (matchScore) {
return (matchScore.fk_matchID === matchScoreData.fk_matchID) && (matchScore.fk_teamID != matchScoreData.fk_teamID);
});
console.log(againstMatchDetail);
所以我想从同一比赛(相同比赛ID)的不同局对象中添加一个团队totalRun
因此,19871987团队的总得分为485 404 + 81,而198752团队的得分为295 280 + 15
因此,团队198708的得分为485,得分为295, 小组198752的得分为295,反对为485
我得到了你们给出的答案,但是我该如何将我的答案换成车队的totalRun,以便与奔跑socred相对应,例如
“ 198708”:295, “ 198752”:485,
我在数组中也有一些类似的匹配,如示例所示。所以我需要在matchID中显示它。
答案 0 :(得分:1)
您需要遍历数组并找到正确的团队ID,然后将团队ID插入具有分数的新对象中。如果对象中已经有团队ID,则添加分数。 以下是为您准备的虚拟逻辑。
var match_results = [
{
"fk_matchID": 234017,
"fk_teamID": 198708,
"inning": 1,
"isDeclare": 0,
"isForfeited": 0,
"isFollowOn": 0,
"isAllOut": 0,
"totalRun": 404,
"totalWicket": 10
},
{
"fk_matchID": 234017,
"fk_teamID": 198752,
"inning": 2,
"isDeclare": 0,
"isForfeited": 0,
"isFollowOn": 0,
"isAllOut": 0,
"totalRun": 280,
"totalWicket": 10
},
{
"fk_matchID": 234017,
"fk_teamID": 198708,
"inning": 3,
"isDeclare": 1,
"isForfeited": 0,
"isFollowOn": 0,
"isAllOut": 0,
"totalRun": 81,
"totalWicket": 4
},
{
"fk_matchID": 234017,
"fk_teamID": 198752,
"inning": 4,
"isDeclare": 0,
"isForfeited": 0,
"isFollowOn": 0,
"isAllOut": 0,
"totalRun": 15,
"totalWicket": 0
}];
var desired_result = {};
for (let i = 0; i< match_results.length; i++){
// console.log(match_results[i]);
if(desired_result[match_results[i]["fk_teamID"]] == undefined){
desired_result[match_results[i]["fk_teamID"]] = match_results[i]["totalRun"];
}else{
desired_result[match_results[i]["fk_teamID"]] += match_results[i]["totalRun"];
}
}
console.log(desired_result);
使用reduce方法
var match_results = [
{
"fk_matchID": 234017,
"fk_teamID": 198708,
"inning": 1,
"isDeclare": 0,
"isForfeited": 0,
"isFollowOn": 0,
"isAllOut": 0,
"totalRun": 404,
"totalWicket": 10
},
{
"fk_matchID": 234017,
"fk_teamID": 198752,
"inning": 2,
"isDeclare": 0,
"isForfeited": 0,
"isFollowOn": 0,
"isAllOut": 0,
"totalRun": 280,
"totalWicket": 10
},
{
"fk_matchID": 234017,
"fk_teamID": 198708,
"inning": 3,
"isDeclare": 1,
"isForfeited": 0,
"isFollowOn": 0,
"isAllOut": 0,
"totalRun": 81,
"totalWicket": 4
},
{
"fk_matchID": 234017,
"fk_teamID": 198752,
"inning": 4,
"isDeclare": 0,
"isForfeited": 0,
"isFollowOn": 0,
"isAllOut": 0,
"totalRun": 15,
"totalWicket": 0
}];
// using reduce function.
var desired_result_2 = match_results.reduce(function(accu, value, currentIndex, array) {
if(accu[value["fk_teamID"]] == undefined){
accu[value["fk_teamID"]] = value["totalRun"];
}else{
accu[value["fk_teamID"]] += value["totalRun"];
}
return accu;
},{});
console.log(desired_result_2);
答案 1 :(得分:0)
请尝试使用此代码并进行所需的更改
var matchScore = [
{
"fk_matchID": 234017,
"fk_teamID": 198708,
"inning": 1,
"isDeclare": 0,
"isForfeited": 0,
"isFollowOn": 0,
"isAllOut": 0,
"totalRun": 404,
"totalWicket": 10
},
{
"fk_matchID": 234017,
"fk_teamID": 198752,
"inning": 2,
"isDeclare": 0,
"isForfeited": 0,
"isFollowOn": 0,
"isAllOut": 0,
"totalRun": 280,
"totalWicket": 10
},
{
"fk_matchID": 234017,
"fk_teamID": 198708,
"inning": 3,
"isDeclare": 1,
"isForfeited": 0,
"isFollowOn": 0,
"isAllOut": 0,
"totalRun": 81,
"totalWicket": 4
},
{
"fk_matchID": 234017,
"fk_teamID": 198752,
"inning": 4,
"isDeclare": 0,
"isForfeited": 0,
"isFollowOn": 0,
"isAllOut": 0,
"totalRun": 15,
"totalWicket": 0
}
];
var matchScoreCopy = matchScore;
var jsonObj = [];
var obj;
var getJsonObject = function (matchScoreCopy) {
if (matchScoreCopy.length === 0) {
return -1;
}
else {
var elementId = matchScoreCopy[0].fk_teamID;
var totrun = 0;
for(var i = 0; i < matchScoreCopy.length; i++) {
var obj = matchScoreCopy[i];
if (obj.fk_teamID === elementId) {
totrun += obj.totalRun;
matchScoreCopy.splice(i, 1);
if (matchScoreCopy.length === 1) {
i--;
}
}
}
obj = {
'fk_teamID': elementId,
'totalRun': totrun
}
jsonObj.push(obj);
getJsonObject(matchScoreCopy);
}
}
getJsonObject(matchScoreCopy);
console.log(jsonObj);