简体   繁体   English

我需要合并对象。 它们都有相同的键,但数值不同。 我需要获取每个键的值之和

[英]I need to merge objects. They all have the same keys but differing numerical values. I need to get the sum of values for each key

New to JS and programming in general. JS和一般编程新手。 I've tried numerous solutions on SO and other resources and cannot seem to figure this out. 我已经在SO和其他资源上尝试了许多解决方案,但似乎无法弄清楚。 I have twenty or more objects representing user 'votes'. 我有二十个或更多代表用户“投票”的对象。 The all have the same keys and different values depending on the user input. 根据用户的输入,它们都有相同的键和不同的值。 I need to 'tally the votes' by summing the different values and return a single object. 我需要通过合计不同的值来“汇总选票”并返回一个对象。

Example: 例:

{
  "Seattle" : "1",
  "Chicago" : "2",
  "New York" : "3"
}

{
  "Chicago" : "1",
  "New York" : "2",
  "Seattle" : "3"
}

{
  "New York" : "1",
  "Chicago" : "2",
  "Seattle" : "3"
}

I'd like the three objects above to be distilled to this: 我希望将以上三个对象简化为:

{
  "New York" : "6",
  "Seattle" : "7",
  "Chicago" : "5"
}

If you could point me in the right direction I'd greatly appreciate it! 如果您能指出正确的方向,我将不胜感激!

Assuming your votes are in an array, you can reduce it into a single object: 假设您的投票在一个数组中,则可以简化为一个对象:

 const votes = [ { "Seattle" : "1", "Chicago" : "2", "New York" : "3" }, { "Chicago" : "1", "New York" : "2", "Seattle" : "3" }, { "New York" : "1", "Chicago" : "2", "Seattle" : "3" } ]; const tally = votes.reduce((a, e) => { for (const p in e) { a[p] = a[p] + (+e[p]) || +e[p]; } return a; }, {}) console.log(tally); 

Another option with reduce/keys/map which somewhat more functional looking: 另一个带有reduce/keys/map选项看起来更functional

 const objs = [{ "Seattle": "1", "Chicago": "2", "New York": "3" }, { "Chicago": "1", "New York": "2", "Seattle": "3" }, { "New York": "1", "Chicago": "2", "Seattle": "3" } ] var result = objs.reduce((r, c) => Object.keys(r).map((x) => r[x] = +r[x] + (+c[x])) && r) console.log(result) 

暂无
暂无

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

相关问题 需要合并具有相同键但具有不同值的对象 - Need to merge objects with the same keys but different values 我需要获得价值的总和 - I need to get a sum of values 我有一组对象。 我想随机 output 个对象,其值的总和等于八 - I have an array of objects. I want to randomly output objects whose sum of values ​is equal to eight 对象数组的对象。 获取具有相同属性的所有值的长度 - Object of array of objects. Get the length of all values with the same property JavaScript 合并具有相同键的对象并将它们的值相加 - JavaScript merge objects with same key and sum their values 我有一个对象数组,每个对象内都有一个称为分数的键,我需要找到每个分数的总和并将其推入数组 - I have an array of objects, inside each object is key called scores, and I need to find the sum of each score and push it into an array 如何合并并获取两个对象的值之和与对象具有相同的键 - How to merge and get sum of the values two objects has same keys as objects 我有一个对象数组。 我需要删除没有特定属性的行 - I have an array of objects. I need to remove the row which doesn't have a particular property 我需要将一个对象作为参数并返回所有值和键 - I need to take an object as an argument and return all values and keys 对对象键/值使用多种映射类型。 只需要一个来自 union 的值吗? - Using multiple mapped types for objects key/values. Need only one value from union?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM