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

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

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 = {
  &quot;512830&quot;: {
    &quot;compname&quot;: &quot;VNEA Vegas League&quot;,
    &quot;grade&quot;: &quot;&quot;,
    &quot;hometeamlabel&quot;: &quot;Pool Tang Clan&quot;,
    &quot;homeshortlabel&quot;: &quot;Pool Tang Clan&quot;,
    &quot;awayteamlabel&quot;: &quot;All Shades&quot;,
    &quot;awayshortlabel&quot;: &quot;All Shades&quot;,
    &quot;homescore&quot;: 11,
    &quot;homescorepoints&quot;: &quot;187&quot;,
    &quot;homeframepointsadj&quot;: &quot;5&quot;,
    &quot;awayscore&quot;: 14,
    &quot;awayscorepoints&quot;: &quot;178&quot;,
    &quot;awayframepointsadj&quot;: &quot;0&quot;,
  }
};

const calculateScore = (data, type) =&gt; 
  Object.values(data).reduce((acc, curr) =&gt; 
    acc + parseInt(curr[`${type}scorepoints`]) + parseInt(curr[`${type}framepointsadj`]), 0);

const homeScore = calculateScore(jsonData, &#39;home&#39;);
const awayScore = calculateScore(jsonData, &#39;away&#39;);

console.log(homeScore);
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:

确定