对象数组中匹配键的总和

时间:2019-04-26 20:51:43

标签: javascript arrays object

问题

我需要对嵌套对象数组中匹配键的值求和。每个对象内有两个子对象。我需要对第二个嵌套对象的值求和。您的帮助将不胜感激!我知道还有其他类似的问题,但是他们的输入格式不同,所以处理他们的示例给我带来了很多麻烦,因为我是JS新手

求和数组

在数组中有两个以上的对象,为简洁起见,我仅包括了两个。

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"
    },
]

所需的输出

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
}

我尝试过的

我尝试了带有地图的化简器,也尝试了for循环,但是由于对象非常深地嵌套在数组中,并且数组中还有第二个对象,我遇到了麻烦不需要产生错误。

3 个答案:

答案 0 :(得分:4)

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);

答案 1 :(得分:0)

这是我要怎么做:

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)

答案 2 :(得分:0)

如果数据来自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 )