简体   繁体   中英

Finding sum of keys in json having same values

So I have this array containing information of a cricket tournament. I want to display totalRun scored by a team in a match, also showing that totalRun as against of the other team.

Array contains

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

SO I want to add a team totalRun from different innings object in same match(same match id)

So team 198708 totalRun will be 485 404+81 and team 198752 score will be 295 280+15

So team 198708 For Score will be 485 and against will be 295 & team 198752 For Score will be 295 and against will be 485

I get the answer given by you guys, but how could I swap my answer for the totalRun of the teams so that it shows against run socred ie

"198708": 295, "198752": 485,

also I've several similar matches in the array as in the example. So I need show it within matchID.

You need to iterate over the array and find the correct team ID and then insert the team ID in new object with the score. If the team ID is already there in the object then add the score. Below is the dummy logic for you.

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

Using reduce method

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

Please try this code and make your required changes

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM