How to sum objects properties inside objects?

  • A+
Category:Languages

I am trying to sum the total of matches and participants of each cup in this object:

{     "Cup_1": {         "bronze": {             "matches": 3,             "participants": 289         },         "silver": {             "matches": 20,             "participants": 1874         },         "gold": {             "matches": 35,             "participants": 3227         },         "platinum": {             "matches": 3,             "participants": 294         },         "diamond": {             "matches": 5,             "participants": 482         },         "ace": {             "matches": 6,             "participants": 574         }     },     "Cup_2": {         "bronze": {             "matches": 17,             "participants": 1609         },         "silver": {             "matches": 46,             "participants": 4408         },         "gold": {             "matches": 157,             "participants": 14391         },         "platinum": {             "matches": 0,             "participants": 0         },         "diamond": {             "matches": 5,             "participants": 469         },         "ace": {             "matches": 10,             "participants": 959         }     },     "Cup_3": {         "bronze": {             "matches": 35,             "participants": 3358         },         "silver": {             "matches": 96,             "participants": 9069         },         "gold": {             "matches": 313,             "participants": 29527         },         "platinum": {             "matches": 10,             "participants": 960         },         "diamond": {             "matches": 16,             "participants": 1538         },         "ace": {             "matches": 45,             "participants": 4280         }     },     "Cup_4": {         "bronze": {             "matches": 2,             "participants": 187         },         "silver": {             "matches": 8,             "participants": 742         },         "gold": {             "matches": 37,             "participants": 3416         },         "platinum": {             "matches": 0,             "participants": 0         },         "diamond": {             "matches": 2,             "participants": 196         },         "ace": {             "matches": 3,             "participants": 290         }     },     "Cup_5": {         "bronze": {             "matches": 89,             "participants": 1638         }     } } 

I don't know how to sum properties when the object has another object inside it (sorry I didn't learned that yet);

I want to have a new object with the results of each cup total matches, and total participants. And also the total matches and participants of each category for all the cups except the cup_5 because it only have a single category.

Something like:

Cup_1 total matches: 72

Cup_1 total participants: 6740

Total Bronze matches (All cups): 57;

Total Bronze Participants (All cups): 5443

Example new object with the results:

var cups_result = {   "Total_Cup_1": {     matches: 72,     participants: 6740,   },   "Total_Bronze": {     matches: 57,     participants: 5443,   }, } 

I read that this sum of objects properties could be done with .map but I don't know how to properly use map with objects.

If anyone could be kind to help me I would be grateful!

 


I would still separate the totals per cup from the totals per medal, and maybe add a grand total as well (since we are at it):

const data = {"Cup_1": {"bronze": {"matches": 3,"participants": 289},"silver": {"matches": 20,"participants": 1874},"gold": {"matches": 35,"participants": 3227},"platinum": {"matches": 3,"participants": 294},"diamond": {"matches": 5,"participants": 482},"ace": {"matches": 6,"participants": 574}},"Cup_2": {"bronze": {"matches": 17,"participants": 1609},"silver": {"matches": 46,"participants": 4408},"gold": {"matches": 157,"participants": 14391},"platinum": {"matches": 0,"participants": 0},"diamond": {"matches": 5,"participants": 469},"ace": {"matches": 10,"participants": 959}},"Cup_3": {"bronze": {"matches": 35,"participants": 3358},"silver": {"matches": 96,"participants": 9069},"gold": {"matches": 313,"participants": 29527},"platinum": {"matches": 10,"participants": 960},"diamond": {"matches": 16,"participants": 1538},"ace": {"matches": 45,"participants": 4280}},"Cup_4": {"bronze": {"matches": 2,"participants": 187},"silver": {"matches": 8,"participants": 742},"gold": {"matches": 37,"participants": 3416},"platinum": {"matches": 0,"participants": 0},"diamond": {"matches": 2,"participants": 196},"ace": {"matches": 3,"participants": 290}},"Cup_5": {"bronze": {"matches": 89,"participants": 1638}}}  const cupTotals = {},        medalTotals = {},        grandTotal = { matches: 0, participants: 0 }; for (const cup in data) {     cupTotals[cup] = { matches: 0, participants: 0 };     for (let medal in data[cup]) {         const {matches, participants} = data[cup][medal];         if (cup === "Cup_5" && medal === "bronze") medal = "junior";         cupTotals[cup].matches += matches;         cupTotals[cup].participants += participants;         if (!medalTotals[medal]) medalTotals[medal] = { matches: 0, participants: 0 };         medalTotals[medal].matches += matches;         medalTotals[medal].participants += participants;         grandTotal.matches += matches;         grandTotal.participants += participants;     } } const results = { cupTotals, medalTotals, grandTotal };  console.log(results);

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: