简体   繁体   English

获取嵌套object中未知键值的总和

[英]Get the sum of values of unknown keys in nested object

I'm trying to add the total values of Par and Tees, where Tees is a nested object. The values seem to be getting overwritten even though I'm adding to the reduce array.我正在尝试添加 Par 和 Tees 的总值,其中 Tees 是嵌套的 object。即使我正在添加到 reduce 数组,这些值似乎也被覆盖了。 The Par and Handicap are working correctly so it seems the second Object forEach is causing some issues by not looping and saving correctly. Par 和 Handicap 工作正常,因此似乎第二个 Object forEach 因未正确循环和保存而导致一些问题。

const getTotals = arr => arr.reduce((total, obj) => {
  Object.entries(obj).forEach(([key1, value]) => {
    if (key1 === 'Par') total[key1] = (total[key1] || 0) + value;
    if (key1 === 'Handicap') total[key1] = '-';
    if (key1 === 'tees') {
      Object.entries(obj.tees).forEach(([key2, value]) => {
        total[key1] = {
          [key2]: (obj.tees[key2] || 0) + value
        }
      })
    }
  })
  return total
}, {})

-- --

let scorecard = [{
    "Hole": 1,
    "Par": 4,
    "tees": {
      "Blue": 372,
      "White": 0,
      "Red_Intermediate": 301,
      "gold": 0
    },
    "Handicap": 10
  },
  {
    "Hole": 2,
    "Par": 4,
    "tees": {
      "Blue": 394,
      "White": 0,
      "Red_Intermediate": 370,
      "gold": 0
    },
    "Handicap": 4
  },
  {
    "Hole": 3,
    "Par": 4,
    "tees": {
      "Blue": 369,
      "White": 0,
      "Red_Intermediate": 248,
      "gold": 0
    },
    "Handicap": 5
  },
  {
    "Hole": 4,
    "Par": 4,
    "tees": {
      "Blue": 361,
      "White": 0,
      "Red_Intermediate": 275,
      "gold": 0
    },
    "Handicap": 18
  },
  {
    "Hole": 5,
    "Par": 4,
    "tees": {
      "Blue": 297,
      "White": 0,
      "Red_Intermediate": 260,
      "gold": 0
    },
    "Handicap": 16
  },
  {
    "Hole": 6,
    "Par": 3,
    "tees": {
      "Blue": 188,
      "White": 0,
      "Red_Intermediate": 167,
      "gold": 0
    },
    "Handicap": 13
  },
  {
    "Hole": 7,
    "Par": 4,
    "tees": {
      "Blue": 342,
      "White": 0,
      "Red_Intermediate": 245,
      "gold": 0
    },
    "Handicap": 12
  },
  {
    "Hole": 8,
    "Par": 3,
    "tees": {
      "Blue": 184,
      "White": 0,
      "Red_Intermediate": 99,
      "gold": 0
    },
    "Handicap": 17
  },
  {
    "Hole": 9,
    "Par": 5,
    "tees": {
      "Blue": 570,
      "White": 0,
      "Red_Intermediate": 452,
      "gold": 0
    },
    "Handicap": 1
  }
]

-- --

Expected output (estimated values):预期 output(估计值):

let output = [{
  "Par": 35,
  "tees" {
    "Blue": 7200,
    "White": 0,
    "Red_Intermediate": 6300,
    "gold": 0
  },
  "Handicap": '-'
}]

I tried your code it seems you are overwriting the value for 'tees' in each iteration of the second array.我尝试了您的代码,您似乎在第二个数组的每次迭代中都覆盖了“tees”的值。 here is one way to do it.这是一种方法。

const getTotals = arr => arr.reduce((total, obj) => {
    Object.entries(obj).forEach(([key1, value]) => {
        if (key1 === 'Par') total[key1] = (total[key1] || 0) + value;
        if (key1 === 'Handicap') total[key1] = '-';
        if (key1 === 'tees') {
            if (total['tees'] == undefined) {
                total['tees'] = {};
            }
            Object.entries(obj.tees).forEach(([key2, value]) => {
                total['tees'][key2] = (total['tees'][key2] == undefined)?
                   value : total['tees'][key2] + value;
            })
        }
    })
    return total
}, {})

This did give the output in the desired format but the data wasn't the same as what you posted in expected.这确实以所需格式提供了 output,但数据与您发布的预期不同。 Below is the output I got and I added the count it matches to the logic (sum of all blue and Red_Intermediate is correct).下面是我得到的 output,我将它匹配的计数添加到逻辑中(所有蓝色和 Red_Intermediate 的总和是正确的)。

{
  Handicap: "-",
  Par: 35,
  tees: {
    Blue: 3077,
    White: 0,
    Red_Intermediate: 2417,
    gold: 0
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM