英文:
How do I get the sum of two json objects?
问题
这是我有的JSON数据。我需要计算 'homescorepoints' + 'homeframepointsadj' 和/或 'awayscorepoints' + 'awayframepointsadj' 的总和...
"512830": {
"compname": "VNEA Vegas League",
"grade": "",
"hometeamlabel": "Pool Tang Clan",
"homeshortlabel": "Pool Tang Clan",
"awayteamlabel": "All Shades",
"awayshortlabel": "All Shades",
"homescore": 11,
"homescorepoints": "187",
"homeframepointsadj": "5",
"awayscore": 14,
"awayscorepoints": "178",
"awayframepointsadj": "0",
}
我理解了基本数组。用于添加多个出现的 'awayscore',但我对将两个不同对象的值相加有困难。
英文:
This is the json data I have. I need the sum of 'homescorepoints' + 'homeframepointsadj' and/or
'awayscorepoints' + 'awayframepointsadj'...
"512830": {
"compname": "VNEA Vegas League",
"grade": "",
"hometeamlabel": "Pool Tang Clan",
"homeshortlabel": "Pool Tang Clan",
"awayteamlabel": "All Shades",
"awayshortlabel": "All Shades",
"homescore": 11,
"homescorepoints": "187",
"homeframepointsadj": "5",
"awayscore": 14,
"awayscorepoints": "178",
"awayframepointsadj": "0",
}
I understand the basic array. Reduce for adding multiple occurrences of say "awayscore", but I'm have a mental block with adding two separate objects values together.
答案1
得分: 0
假设您想要从此示例对象中累加值:.reduce()
接受数组,所以您可以使用 Object.values()
方法获取对象的数组,然后像这样使用 .reduce()
:
const jsonData = {
"512830": {
"compname": "VNEA Vegas League",
"grade": "",
"hometeamlabel": "Pool Tang Clan",
"homeshortlabel": "Pool Tang Clan",
"awayteamlabel": "All Shades",
"awayshortlabel": "All Shades",
"homescore": 11,
"homescorepoints": "187",
"homeframepointsadj": "5",
"awayscore": 14,
"awayscorepoints": "178",
"awayframepointsadj": "0",
}
};
const calculateScore = (data, type) =>
Object.values(data).reduce((acc, curr) =>
acc + parseInt(curr[`${type}scorepoints`]) + parseInt(curr[`${type}framepointsadj`]), 0);
const homeScore = calculateScore(jsonData, 'home');
const awayScore = calculateScore(jsonData, 'away');
console.log(homeScore);
console.log(awayScore);
这段代码将计算主队和客队的得分并将其打印出来。
英文:
Assuming that you wanted to sum values from this example object: .reduce() accept arrays so, you can use Object.values() method for your object to get the array and than use .reduce() like this:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const jsonData = {
"512830": {
"compname": "VNEA Vegas League",
"grade": "",
"hometeamlabel": "Pool Tang Clan",
"homeshortlabel": "Pool Tang Clan",
"awayteamlabel": "All Shades",
"awayshortlabel": "All Shades",
"homescore": 11,
"homescorepoints": "187",
"homeframepointsadj": "5",
"awayscore": 14,
"awayscorepoints": "178",
"awayframepointsadj": "0",
}
};
const calculateScore = (data, type) =>
Object.values(data).reduce((acc, curr) =>
acc + parseInt(curr[`${type}scorepoints`]) + parseInt(curr[`${type}framepointsadj`]), 0);
const homeScore = calculateScore(jsonData, 'home');
const awayScore = calculateScore(jsonData, 'away');
console.log(homeScore);
console.log(awayScore);
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论