object array Group by an element?

前端 未结 5 992
面向向阳花
面向向阳花 2020-12-30 14:42

Please see this example: JsFiddle

Question: I have the following JSON Array

y= [ {\"LngTrend\":15,\"DblValue\":10,\"DtmStamp\":13582260         


        
5条回答
  •  生来不讨喜
    2020-12-30 15:04

     var y = [
       {"LngTrend":15,"DblValue":10,"DtmStamp":1358226000000},     
       {"LngTrend":16,"DblValue":92,"DtmStamp":1358226000000},    
       {"LngTrend":17,"DblValue":45,"DtmStamp":1358226000000},
       {"LngTrend":18,"DblValue":87,"DtmStamp":1358226000000},
       {"LngTrend":15,"DblValue":10,"DtmStamp":1358226060000},
       {"LngTrend":16,"DblValue":87,"DtmStamp":1358226060000},
       {"LngTrend":17,"DblValue":45,"DtmStamp":1358226060000},
       {"LngTrend":18,"DblValue":92,"DtmStamp":1358226060000},
     ];
    var x = {};
    
    for(var k in y){
      if(x[y[k]["DtmStamp"]] == undefined)
         x[y[k]["DtmStamp"]] = [];
         x[y[k]["DtmStamp"]].push(y[k]["DblValue"])
    }
    alert(JSON.stringify(x))
    console.log(x);
    

    See http://plnkr.co/edit/511sKSdzHGYuvpYqKCPD?p=preview

提交回复
热议问题