簡體   English   中英

在具有相同值的json中查找鍵的總和

[英]Finding sum of keys in json having same values

所以我有一個包含板球比賽信息的數組。 我想顯示一個團隊在一場比賽中得分的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); 

所以我想從同一比賽中的不同局對象添加一個團隊totalRun(相同的比賽ID)

因此,團隊198708的總跑步次數將為485 404 + 81,而團隊198752的得分將為295 280 + 15

因此,團隊198708的得分為485,反對將為295,而小組198752的得分為295,反對將為485

我得到了你們給出的答案,但是我該如何將我的答案換成車隊的totalRun,以便與run socred即

“ 198708”:295,“ 198752”:485,

在示例中,我在數組中也有一些相似的匹配項。 所以我需要在matchID中顯示它。

您需要遍歷數組並找到正確的團隊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); 

使用減少方法

 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); 

請嘗試此代碼並進行所需的更改

  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); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM