如何在 JavaScript 中计算数组的总和

发布于 2025-01-11 00:41:26 字数 614 浏览 0 评论 0原文

我想在 JavaScript 中计算数组数据的总和。

数组

[
  {
    red_team: {
      count: 2,
      money: 3000,
    },
    blue_team: {
      count: 10,
      money: 100000,
    },
  },
  {
    red_team: {
      count: 1,
      money: 1000,
    },
    blue_team: {
      count: 100,
      money: 200000,
    },
  }
]

预期结果

{
  red_team: {
    count: 3,
    money: 4000,
  },
  blue_team: {
    count: 110,
    money: 300000,
  },
}

注意:red_team& blue_team 是一个枚举。

我怎样才能计算它?

I want to calculate sum of array data in JavaScript.

Array :

[
  {
    red_team: {
      count: 2,
      money: 3000,
    },
    blue_team: {
      count: 10,
      money: 100000,
    },
  },
  {
    red_team: {
      count: 1,
      money: 1000,
    },
    blue_team: {
      count: 100,
      money: 200000,
    },
  }
]

Expected Result :

{
  red_team: {
    count: 3,
    money: 4000,
  },
  blue_team: {
    count: 110,
    money: 300000,
  },
}

Note : red_team & blue_team is an Enum.

how can I calculate it ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尛丟丟 2025-01-18 00:41:26

将数组展开到 lodash 的 _.mergeWith() 中,并创建一个对数字求和的定制器,但对于所有其他类型返回 undefined,以便 lodash 能够处理所有其他情况:

const data = [{"red_team":{"count":2,"money":3000},"blue_team":{"count":10,"money":100000}},{"red_team":{"count":1,"money":1000},"blue_team":{"count":100,"money":200000}}]


const result = _.mergeWith({}, ...data, (a, b) => 
  _.isNumber(a) ? a + b : undefined
)

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js" integrity="sha512-WFN04846sdKMIP5LKNphMaWzU7YpMyCU245etK3g/2ARYbPK9Ub18eG+ljU96qKRCWh+quCY7yefSmlkQw1ANQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

Spread the array into lodash's _.mergeWith(), and create a customizer that sums number, but returns undefined for all other types, so that lodash would handle all other cases:

const data = [{"red_team":{"count":2,"money":3000},"blue_team":{"count":10,"money":100000}},{"red_team":{"count":1,"money":1000},"blue_team":{"count":100,"money":200000}}]


const result = _.mergeWith({}, ...data, (a, b) => 
  _.isNumber(a) ? a + b : undefined
)

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js" integrity="sha512-WFN04846sdKMIP5LKNphMaWzU7YpMyCU245etK3g/2ARYbPK9Ub18eG+ljU96qKRCWh+quCY7yefSmlkQw1ANQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

黯然#的苍凉 2025-01-18 00:41:26

根据上面的评论,有无限种方法可以做到这一点。我刚刚想出了一个(诚然相当冗长)

详细的解决方案:

const arr = [
  {
    red_team: {
      count: 2,
      money: 3000,
    },
    blue_team: {
      count: 10,
      money: 100000,
    },
  },
  {
    red_team: {
      count: 1,
      money: 1000,
    },
    blue_team: {
      count: 100,
      money: 200000,
    },
  }
];

function sumByKeyAndProperty(arr, key, property){
  let sum = 0
  arr.forEach((obj) => {
    sum+=obj[key][property]
  })
  return sum
}

returnObj = {
  red_team: {
    count: sumByKeyAndProperty(arr, 'red_team', 'count'),
    money: sumByKeyAndProperty(arr, 'red_team', 'money'),
  },
  blue_team: {
    count: sumByKeyAndProperty(arr, 'blue_team', 'count'),
    money: sumByKeyAndProperty(arr, 'blue_team', 'money'),
  }
}

console.log(returnObj)

Per a comment above, an infinite ways of doing this. I just came up with one (rather verbose admittedly)

Verbose solution:

const arr = [
  {
    red_team: {
      count: 2,
      money: 3000,
    },
    blue_team: {
      count: 10,
      money: 100000,
    },
  },
  {
    red_team: {
      count: 1,
      money: 1000,
    },
    blue_team: {
      count: 100,
      money: 200000,
    },
  }
];

function sumByKeyAndProperty(arr, key, property){
  let sum = 0
  arr.forEach((obj) => {
    sum+=obj[key][property]
  })
  return sum
}

returnObj = {
  red_team: {
    count: sumByKeyAndProperty(arr, 'red_team', 'count'),
    money: sumByKeyAndProperty(arr, 'red_team', 'money'),
  },
  blue_team: {
    count: sumByKeyAndProperty(arr, 'blue_team', 'count'),
    money: sumByKeyAndProperty(arr, 'blue_team', 'money'),
  }
}

console.log(returnObj)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文