[英]Sum values of matching keys in array of objects
Problem 问题
I need to sum the values of matching keys in a nested array of objects. 我需要对嵌套对象数组中匹配键的值求和。 There are two sub objects inside each object.
每个对象内有两个子对象。 I need to sum the values of the second nest object.
我需要对第二个嵌套对象的值求和。 Your help would be greatly appreciated!
您的帮助将不胜感激! I know there are other similar questions but their inputs are formatted differently so working off their examples is giving me a lot of trouble since I'm new to JS
我知道还有其他类似的问题,但是他们的输入格式不同,所以处理他们的示例给我带来了很多麻烦,因为我是JS新手
Array to sum 求和数组
It has more than two objects in the array but I only included two for brevity. 它在数组中有两个以上的对象,但是为了简洁起见,我仅包括了两个。
stats: [
0: {
statsFields: {
duo: 2
duoM: 2
duoW: 2
kdr: 2
solo: 2
soloM: 2
soloW: 2
squad: 2
squadM: 2
squadW: 2
total: 2
totalM: 1
totalW: 2
winP: 2
},
_id: "5cc283ba7b752f322ce26168"
},
1: {
statsFields: {
duo: 2
duoM: 2
duoW: 2
kdr: 2
solo: 2
soloM: 2
soloW: 2
squad: 2
squadM: 2
squadW: 2
total: 2
totalM: 1
totalW: 2
winP: 2
},
_id: "5cc284cd7b752f322ce26169"
},
]
Desired Output 期望的输出
statsFields: {
duo: 4
duoM: 4
duoW: 4
kdr: 4
solo: 4
soloM: 4
soloW: 4
squad: 4
squadM: 4
squadW: 4
total: 4
totalM: 2
totalW: 4
winP: 4
}
What I've tried 我尝试过的
I've tried a reducer with a map and I've also tried a for loop but I'm having trouble because the object is so deeply nested in the array and there is also a second object in the array I don't need which is producing errors. 我已经尝试过使用map的reducer,也曾尝试过for循环,但是我遇到了麻烦,因为该对象非常深地嵌套在数组中,并且数组中还有另一个我不需要的对象产生错误。
let stats = [{ statsFields: { duo: 2, duoM: 2, duoW: 2, kdr: 2, solo: 2, soloM: 2, soloW: 2, squad: 2, squadM: 2, squadW: 2, total: 2, totalM: 1, totalW: 2, winP: 2, }, _id: "5cc283ba7b752f322ce26168", }, { statsFields: { duo: 2, duoM: 2, duoW: 2, kdr: 2, solo: 2, soloM: 2, soloW: 2, squad: 2, squadM: 2, squadW: 2, total: 2, totalM: 1, totalW: 2, winP: 2, }, _id: "5cc284cd7b752f322ce26169", }]; let sum = stats.reduce((acc, {statsFields}) => { Object.entries(statsFields).forEach(([key, value]) => acc[key] = (acc[key] || 0) + value); return acc; }, {}); console.log(sum);
Here is how I'll do this: 这是我的操作方法:
var ary = { stats: [{ statsFields: { duo: 2, duoM: 2, duoW: 2, kdr: 2, solo: 2, soloM: 2, soloW: 2, squad: 2, squadM: 2, squadW: 2, total: 2, totalM: 1, totalW: 2, winP: 2, }, _id: "5cc283ba7b752f322ce26168" }, { statsFields: { duo: 2, duoM: 2, duoW: 2, kdr: 2, solo: 2, soloM: 2, soloW: 2, squad: 2, squadM: 2, squadW: 2, total: 2, totalM: 1, totalW: 2, winP: 2, }, _id: "5cc284cd7b752f322ce26169" }, ] } var res = ary.stats.reduce((acc, cur) => { var keys = Object.keys(cur.statsFields) return keys.reduce((mem, key) => { mem[key] = (acc.statsFields[key] || 0) +( cur.statsFields[key] || 0); return mem; }, {}) }); console.log(res)
If the data is from a JSON string, it can also be done during parsing : 如果数据来自JSON字符串,则也可以在解析过程中完成:
var result = {}, json = '{"stats":[{"statsFields":{"duo":2,"duoM":2,"duoW":2,"kdr":2,"solo":2,"soloM":2,"soloW":2,"squad":2,"squadM":2,"squadW":2,"total":2,"totalM":1,"totalW":2,"winP":2},"_id":"5cc283ba7b752f322ce26168"},{"statsFields":{"duo":2,"duoM":2,"duoW":2,"kdr":2,"solo":2,"soloM":2,"soloW":2,"squad":2,"squadM":2,"squadW":2,"total":2,"totalM":1,"totalW":2,"winP":2},"_id":"5cc284cd7b752f322ce26169"}]}' JSON.parse(json, (k, v) => v.toFixed && (result[k] = result[k] + v || v)) console.log( result )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.