猿问

js操作json

   var Data1 = [

         { time: 1440491658, a: 1 },

         { time: 1440491668, a: 3 },

         { time: 1440491668, b: 5 },

         { 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 Data2 = [

         { time: 1440491658, a: 1 },

         { 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 }

         ];

         

        //如何将data1的json转换成data2的格式??


慕工程0101907
浏览 454回答 1
1回答

万千封印

var Data1 = [&nbsp; &nbsp; &nbsp;{ time: 1440491658, a: 1 },&nbsp; &nbsp; &nbsp;{ time: 1440491668, a: 3 },&nbsp; &nbsp; &nbsp;{ time: 1440491668, b: 4 },&nbsp; &nbsp; &nbsp;{ time: 1440491668, c: 7 },&nbsp; &nbsp; &nbsp;{ time: 1440491668, d: 12 },&nbsp; &nbsp; &nbsp;{ time: 1440491678, a: 11 },&nbsp; &nbsp; &nbsp;{ time: 1440491678, b: 88 },&nbsp; &nbsp; &nbsp;{ time: 1440491678, c: 13 },&nbsp; &nbsp; &nbsp;{ time: 1440491678, d: 100 },&nbsp; &nbsp; &nbsp;{ time: 1440491688, a: 11 },&nbsp; &nbsp; &nbsp;{ time: 1440491688, b: 69 },&nbsp; &nbsp; &nbsp;{ time: 1440491688, c: 12 },&nbsp; &nbsp; &nbsp;{ time: 1440491688, d: 73 }&nbsp; &nbsp; &nbsp;];var map = {},arr = [];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for(var i=0;i<Data1.length;i++){&nbsp; &nbsp; if(map[Data1[i].time]===undefined){&nbsp; &nbsp; &nbsp; &nbsp; map[Data1[i].time] = Object.assign({},Data1[i]);&nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; Object.assign(map[Data1[i].time],Data1[i])&nbsp; &nbsp; }}for(var key in map){&nbsp; &nbsp; arr.push( map[key] )}console.dir(arr)我想问下,你问题中的Data2是不是写错了,因为你data1中,根本没有3,5这两个数字出现var Data2 = [&nbsp;{ time: 1440491658, a: 1 },//下面的是不是有问题,data1中都没有{time: 1440491668,a:3}和{time: 1440491668,b:5}&nbsp;{ time: 1440491668, a: 3, b: 5, c: 7, d: 12 },&nbsp;{ time: 1440491678, a: 11, b: 88, c: 13, d: 100},&nbsp;{ time: 1440491688, a: 11, b: 69, c: 12, d: 73 }&nbsp;];难道是我理解错题目意思了?
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答