合并两个对象数组,同时对特定键求和
Merge two arrays of objects while summing for a particular key
我有两个数组
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 }]
如何合并这两个数组,使生成的数组计数和交互按团队相加并合并交互的位置。_id和采样日期保持不变
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>');
带下划线:
g = _.groupBy(
_.union(array1, array2),
e => e._id.month + '/' + e._id.year
);
r = _.map(g, arrays => _.reduce(arrays, (x, y) => {
x.count += y.count;
x.interactions = x.interactions.concat(y.interactions);
x.interactionsByTeam += y.interactionsByTeam;
return x;
})
)
基本上
- 连接两个数组
- 将具有相等键的子数组组合在一起
- 对于每个组,迭代子数组并计算总计对象
假设两个数组的大小相同,
arrayResult = array1.map((obj, i) => (
{
_id: obj._id,
sampleDate: obj.sampleDate,
interactionsBySelf: obj.interactionsBySelf,
interactionsByTeam: obj.interactionsByTeam + array2[i].interactionsByTeam,
interactions: obj.interactions.concat(array2[i].interactions),
}
)
相关文章:
- 如何按对象数组进行分组和求和
- 求和d3对象数组上的元素
- 如何使用数组减少来求和对象属性长度,不带类型
- 求和并组织数组
- 用于从包含最大求和的整数数组中提取子数组的算法
- JavaScript 对多维数组元素值求和
- 合并两个对象数组,同时对特定键求和
- 对关联数组键值求和并按键分组
- 如何在 AngularJS ng-repeat中对 Json 数组值求和
- 数组中对象的值求和
- 在对象数组中对具有相同对象属性 B 的 JavaScript 对象属性 A 值求和
- 只是尝试对 JavaScript 数组的 2 个特定元素求和
- 对 JavaScript 数组重复值求和
- 如何对文本框数组中存储的值求和
- 为什么数组和对象([ ] + {})的求和是javascript中的一个字符串
- 如何合并两个对象数组,并在 JavaScript 中对重复对象键的值求和
- JS-数组的求和元素
- 对对应于另一个数组的数组的值求和
- 如何使用Ramda通过索引对n数组求和
- 对JSON数组响应中的值求和