Javascript 合并两个对象数组,同时对特定键求和
我有两个数组Javascript 合并两个对象数组,同时对特定键求和,javascript,arrays,underscore.js,array-merge,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 }, c
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 }]
及
如何合并这两个数组,使生成的数组
其中count和interactionsByteam相加,交互合并。
_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 }]
假设两个数组的大小相同
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),
}
)
假设两个数组的大小相同
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),
}
)
这是一个与临时对象合并和计数的建议,并且
var array1=[{u id:{month:9,year:2015},count:1,sampleDate:'Tue Sep 22 2015 20:04:46 GMT+0530(IST)',interactions:['[Object1]',interactions by self:0,interactions by team:1},{u id:{month:10,year:2015},count 5,sampleDate:'Thu Oct 01 2015 18:08:24 GMT+0530(IST)',interactions:['[Object2],[Object2],[Object Object 1],interactions:[Object,[Object]'],interactionsBySelf:0,interactionsByTeam:5},{u id:{month:11,year:2015}},
array2=[{u id:{month:9,year:2015},计数:3,样本日期:{u id:{month:10,year:2015},计数:7,样本日期:'Thu-Oct 01 2015 18:08:24 GMT+0530(IST)',[Object]、[Object]、[Object]、[Object]']、interactionsBySelf:0、interactionsByTeam:5}、{U id:{月份:11、年份:2015}},
结果=函数(数组){
变量o={},r=[];
array.forEach(函数(a){
var k=年+月+月;
如果(!(k in o)){
o[k]={
_id:a.(身份证),
计数:0,
样本日期:a.sampleDate,
交互作用:[],
自我互动:a.自我互动,
interactionsByTeam:0
};
r、 推(o[k]);
}
o[k].count+=a.count;
o[k].interactions=o[k].interactions.concat(a.interactions);
o[k].interactionsByTeam+=a.interactionsByTeam;
});
返回r;
}(array1.concat(array2));
document.write(''+JSON.stringify(result,0,4)+'');
这是一个与临时对象合并和计数的建议,并且
var array1=[{u id:{month:9,year:2015},count:1,sampleDate:'Tue Sep 22 2015 20:04:46 GMT+0530(IST)',interactions:['[Object1]',interactions by self:0,interactions by team:1},{u id:{month:10,year:2015},count 5,sampleDate:'Thu Oct 01 2015 18:08:24 GMT+0530(IST)',interactions:['[Object2],[Object2],[Object Object 1],interactions:[Object,[Object]'],interactionsBySelf:0,interactionsByTeam:5},{u id:{month:11,year:2015}},
array2=[{u id:{month:9,year:2015},计数:3,样本日期:{u id:{month:10,year:2015},计数:7,样本日期:'Thu-Oct 01 2015 18:08:24 GMT+0530(IST)',[Object]、[Object]、[Object]、[Object]']、interactionsBySelf:0、interactionsByTeam:5}、{U id:{月份:11、年份:2015}},
结果=函数(数组){
变量o={},r=[];
array.forEach(函数(a){
var k=年+月+月;
如果(!(k in o)){
o[k]={
_id:a.(身份证),
计数:0,
样本日期:a.sampleDate,
交互作用:[],
自我互动:a.自我互动,
interactionsByTeam:0
};
r、 推(o[k]);
}
o[k].count+=a.count;
o[k].interactions=o[k].interactions.concat(a.interactions);
o[k].interactionsByTeam+=a.interactionsByTeam;
});
返回r;
}(array1.concat(array2));
document.write(“”+JSON.stringify(result,0,4)+“”);
带下划线:
基本上
- 将两个阵列连接起来
- 将具有相等键的子数组分组在一起
- 对于每个组,迭代子数组并计算totals对象
- 将两个阵列连接起来
- 将具有相等键的子数组分组在一起
- 对于每个组,迭代子数组并计算totals对象
- 带下划线:
基本上
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;
})
)