Finding sum of keys in json having same values

by codenoobie   Last Updated July 12, 2019 05:26 AM

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



Answers 1


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

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

Kaushik
Kaushik
July 12, 2019 05:19 AM

Related Questions


Updated October 02, 2016 08:11 AM

Updated March 06, 2017 12:26 PM

Updated June 25, 2017 17:26 PM

Updated November 23, 2017 18:26 PM

Updated October 17, 2017 15:26 PM