1 回答
TA贡献1891条经验 获得超3个赞
var Data1 = [
{ time: 1440491658, a: 1 },
{ time: 1440491668, a: 3 },
{ time: 1440491668, b: 4 },
{ time: 1440491668, c: 7 },
{ time: 1440491668, d: 12 },
{ time: 1440491678, a: 11 },
{ time: 1440491678, b: 88 },
{ time: 1440491678, c: 13 },
{ time: 1440491678, d: 100 },
{ time: 1440491688, a: 11 },
{ time: 1440491688, b: 69 },
{ time: 1440491688, c: 12 },
{ time: 1440491688, d: 73 }
];
var map = {},arr = [];
for(var i=0;i<Data1.length;i++){
if(map[Data1[i].time]===undefined){
map[Data1[i].time] = Object.assign({},Data1[i]);
}else{
Object.assign(map[Data1[i].time],Data1[i])
}
}
for(var key in map){
arr.push( map[key] )
}
console.dir(arr)
我想问下,你问题中的Data2是不是写错了,因为你data1中,根本没有3,5这两个数字出现
var Data2 = [
{ time: 1440491658, a: 1 },
//下面的是不是有问题,data1中都没有{time: 1440491668,a:3}和{time: 1440491668,b:5}
{ time: 1440491668, a: 3, b: 5, c: 7, d: 12 },
{ time: 1440491678, a: 11, b: 88, c: 13, d: 100},
{ time: 1440491688, a: 11, b: 69, c: 12, d: 73 }
];
难道是我理解错题目意思了?
添加回答
举报