如何获得两个 JSON 对象的总和?

huangapple go评论101阅读模式
英文:

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'...

  1. "512830": {
  2. "compname": "VNEA Vegas League",
  3. "grade": "",
  4. "hometeamlabel": "Pool Tang Clan",
  5. "homeshortlabel": "Pool Tang Clan",
  6. "awayteamlabel": "All Shades",
  7. "awayshortlabel": "All Shades",
  8. "homescore": 11,
  9. "homescorepoints": "187",
  10. "homeframepointsadj": "5",
  11. "awayscore": 14,
  12. "awayscorepoints": "178",
  13. "awayframepointsadj": "0",
  14. }

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

  1. const jsonData = {
  2. "512830": {
  3. "compname": "VNEA Vegas League",
  4. "grade": "",
  5. "hometeamlabel": "Pool Tang Clan",
  6. "homeshortlabel": "Pool Tang Clan",
  7. "awayteamlabel": "All Shades",
  8. "awayshortlabel": "All Shades",
  9. "homescore": 11,
  10. "homescorepoints": "187",
  11. "homeframepointsadj": "5",
  12. "awayscore": 14,
  13. "awayscorepoints": "178",
  14. "awayframepointsadj": "0",
  15. }
  16. };
  17. const calculateScore = (data, type) =>
  18. Object.values(data).reduce((acc, curr) =>
  19. acc + parseInt(curr[`${type}scorepoints`]) + parseInt(curr[`${type}framepointsadj`]), 0);
  20. const homeScore = calculateScore(jsonData, 'home');
  21. const awayScore = calculateScore(jsonData, 'away');
  22. console.log(homeScore);
  23. 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 -->

  1. const jsonData = {
  2. &quot;512830&quot;: {
  3. &quot;compname&quot;: &quot;VNEA Vegas League&quot;,
  4. &quot;grade&quot;: &quot;&quot;,
  5. &quot;hometeamlabel&quot;: &quot;Pool Tang Clan&quot;,
  6. &quot;homeshortlabel&quot;: &quot;Pool Tang Clan&quot;,
  7. &quot;awayteamlabel&quot;: &quot;All Shades&quot;,
  8. &quot;awayshortlabel&quot;: &quot;All Shades&quot;,
  9. &quot;homescore&quot;: 11,
  10. &quot;homescorepoints&quot;: &quot;187&quot;,
  11. &quot;homeframepointsadj&quot;: &quot;5&quot;,
  12. &quot;awayscore&quot;: 14,
  13. &quot;awayscorepoints&quot;: &quot;178&quot;,
  14. &quot;awayframepointsadj&quot;: &quot;0&quot;,
  15. }
  16. };
  17. const calculateScore = (data, type) =&gt;
  18. Object.values(data).reduce((acc, curr) =&gt;
  19. acc + parseInt(curr[`${type}scorepoints`]) + parseInt(curr[`${type}framepointsadj`]), 0);
  20. const homeScore = calculateScore(jsonData, &#39;home&#39;);
  21. const awayScore = calculateScore(jsonData, &#39;away&#39;);
  22. console.log(homeScore);
  23. console.log(awayScore);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月18日 12:30:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75491201.html
匿名

发表评论

匿名网友

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

确定