如何对对象内的对象属性求和?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何对对象内的对象属性求和?相关的知识,希望对你有一定的参考价值。
我想在这个对象中总计每个杯子的匹配和参与者的总数:
{
"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
}
}
}
当对象里面有另一个对象时,我不知道如何对属性求和(对不起,我还没有学到);
我想要一个新的对象,每个杯子的总比赛结果和参赛者总数。还有除cup_5之外的所有杯子的每个类别的总匹配和参与者,因为它只有一个类别。
就像是:
Cup_1总比赛:72
Cup_1总参与人数:6740
铜牌总数(所有杯子):57;
青铜参与者总数(所有杯子):5443
示例新对象的结果:
var cups_result = {
"Total_Cup_1": {
matches: 72,
participants: 6740,
},
"Total_Bronze": {
matches: 57,
participants: 5443,
},
}
我读到这个对象属性的总和可以用.map
完成,但我不知道如何正确使用map与对象。
如果有人能帮助我,我将不胜感激!
答案
我仍然会将每杯的总数与每枚奖牌的总数分开,也可以加上一个总数(因为我们在这里):
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);
另一答案
嵌套for循环将有助于从cups var中获取匹配和参与者字段。
for (var c in cups) {
for ( let medal in cups[c] ) {
console.log(cups[c][medal]["matches"], cups[c][medal]["participants"]);
}
}
另一答案
您可以使用Object.keys
,Object.values
和destructuring - 下面返回每个杯子的摘要:
const cup_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
}
}
};
let cup_results = {};
Object.keys(cup_data).forEach(key => {
let newCupKey = "Total_" + key;
cup_results[newCupKey] = {};
cup_results[newCupKey].matches = Object.values(cup_data[key]).reduce((acc, {
matches
}) => acc + matches, 0);
cup_results[newCupKey].participants = Object.values(cup_data[key]).reduce((acc, {
participants
}) => acc + participants, 0);
});
console.log(cup_results);
另一答案
为结果创建一个空对象。然后使用forEach()
循环遍历所有杯子。并将结果存储在空对象中。
let 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": 以上是关于如何对对象内的对象属性求和?的主要内容,如果未能解决你的问题,请参考以下文章