javascript - 合并两个对象数组,同时对特定键求和

标签 javascript arrays underscore.js array-merge

我有两个数组

array1 = [ {_id: { month: 9, year: 2015 },
    count: 1,
    sampleDate: Tue Sep 22 2015 20:04:46 GMT+0530 (IST),
    interactions: [ [Object] ],
    interactionsBySelf: 0,
    interactionsByTeam: 1 },
  { _id: { month: 10, year: 2015 },
    count: 5,
    sampleDate: Thu Oct 01 2015 18:08:24 GMT+0530 (IST),
    interactions: [ [Object], [Object], [Object], [Object], [Object] ],
    interactionsBySelf: 0,
    interactionsByTeam: 5 },
  { _id: { month: 11, year: 2015 }]

array2 = [ {_id: { month: 9, year: 2015 },
    count: 3,
    sampleDate: Tue Sep 22 2015 20:04:46 GMT+0530 (IST),
    interactions: [ [Object],[Object],[Object] ],
    interactionsBySelf: 0,
    interactionsByTeam: 1 },
  { _id: { month: 10, year: 2015 },
    count: 7,
    sampleDate: Thu Oct 01 2015 18:08:24 GMT+0530 (IST),
    interactions: [ [Object], [Object], [Object], [Object], [Object], [Object], [Object] ],
    interactionsBySelf: 0,
    interactionsByTeam: 5 },
  { _id: { month: 11, year: 2015 }]

如何合并这两个数组,使得结果数组为 其中计数和交互Byteam 相加并且交互合并。 _id 和sampleDate 保持不变

arrayResult = [{ _id: { month: 9, year: 2015 },
    count: 4,
    sampleDate: Tue Sep 22 2015 20:04:46 GMT+0530 (IST),
    interactions: [ [Object],[Object],[Object],[Object] ],
    interactionsBySelf: 0,
    interactionsByTeam: 4 },
  { _id: { month: 10, year: 2015 },
    count: 12,
    sampleDate: Thu Oct 01 2015 18:08:24 GMT+0530 (IST),
    interactions: [ [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object] ],
    interactionsBySelf: 0,
    interactionsByTeam: 12 },
  { _id: { month: 11, year: 2015 }]

最佳答案

这是一个与临时对象和 Array#forEach() 进行合并和计数的提案.

var array1 = [{ _id: { month: 9, year: 2015 }, count: 1, sampleDate: 'Tue Sep 22 2015 20:04:46 GMT+0530 (IST)', interactions: ['[Object1]'], interactionsBySelf: 0, interactionsByTeam: 1 }, { _id: { month: 10, year: 2015 }, count: 5, sampleDate: 'Thu Oct 01 2015 18:08:24 GMT+0530 (IST)', interactions: ['[Object2], [Object], [Object], [Object], [Object]'], interactionsBySelf: 0, interactionsByTeam: 5 }, { _id: { month: 11, year: 2015 } }],
    array2 = [{ _id: { month: 9, year: 2015 }, count: 3, sampleDate: 'Tue Sep 22 2015 20:04:46 GMT+0530 (IST)', interactions: ['[Object3],[Object],[Object]'], interactionsBySelf: 0, interactionsByTeam: 1 }, { _id: { month: 10, year: 2015 }, count: 7, sampleDate: 'Thu Oct 01 2015 18:08:24 GMT+0530 (IST)', interactions: ['[Object4], [Object], [Object], [Object], [Object], [Object], [Object]'], interactionsBySelf: 0, interactionsByTeam: 5 }, { _id: { month: 11, year: 2015 } }],
    result = function (array) {
        var o = {}, r = [];
        array.forEach(function (a) {
            var k = a._id.year + '|' + a._id.month;
            if (!(k in o)) {
                o[k] = {
                    _id: a._id,
                    count: 0,
                    sampleDate: a.sampleDate,
                    interactions: [],
                    interactionsBySelf: a.interactionsBySelf,
                    interactionsByTeam: 0
                };
                r.push(o[k]);
            }
            o[k].count += a.count;
            o[k].interactions = o[k].interactions.concat(a.interactions);
            o[k].interactionsByTeam += a.interactionsByTeam;
        });
        return r;
    }(array1.concat(array2));

document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');

关于javascript - 合并两个对象数组,同时对特定键求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35790636/

相关文章:

javascript - AngularJS ng-change 和 select 标签的问题

javascript - 是否有一种 document.createElement 多个元素的简写方式?

javascript - 在提交表单之前验证输入字段

javascript - 如何从两个字符串数组制作 JSON - 第一个数组具有值,第二个数组具有相同序列的 ID

arrays - 快速解析数组中的加载数据

javascript - 如何求key的值之和?

Javascript,简化对象转换

javascript - 为什么 _.extend 会进行浅复制?

javascript - 当浏览器窗口展开时,响应 slider 图像被菜单重叠

java - 检查 jTextField 输入是 int 还是 string