如何在angularjs或javascript

时间:2015-07-13 12:25:39

标签: javascript arrays angularjs

我有下面的数组,我想创建另一个数组数组。下面是我的数组

 $scope.data2 = 
                [     
                    {"dt":"07 Jul 2015","avgdelay":"10","code_sent_time":"07 Jul 2015 12:30 PM","reply_received_time":"07 Jul 2015 12:40 PM","time_diff":10},
                    {"dt":"07 Jul 2015","avgdelay":"10","code_sent_time":"07 Jul 2015 02:10 AM","reply_received_time":"07 Jul 2015 02:30 AM","time_diff":20 },
                    {"dt":"07 Jul 2015","avgdelay":"10","code_sent_time":"07 Jul 2015 03:10 AM","reply_received_time":"07 Jul 2015 03:15 AM","time_diff":5 },
                    {"dt":"07 Jul 2015","avgdelay":"10","code_sent_time":"07 Jul 2015 04:45 AM","reply_received_time":"07 Jul 2015 05:00 AM","time_diff":15 },
                    {"dt":"08 Jul 2015","avgdelay":"20","code_sent_time":"08 Jul 2015 12:30 PM","reply_received_time":"08 Jul 2015 12:40 PM","time_diff":35},   
                    {"dt":"08 Jul 2015","avgdelay":"20","code_sent_time":"08 Jul 2015 02:10 AM","reply_received_time":"08 Jul 2015 02:30 AM","time_diff":42 },
                    {"dt":"08 Jul 2015","avgdelay":"20","code_sent_time":"08 Jul 2015 03:10 AM","reply_received_time":"08 Jul 2015 03:15 AM","time_diff":5 },
                    {"dt":"08 Jul 2015","avgdelay":"20","code_sent_time":"08 Jul 2015 04:45 AM","reply_received_time":"08 Jul 2015 05:00 AM","time_diff":5 },
                    {"dt":"09 Jul 2015","avgdelay":"30","code_sent_time":"09 Jul 2015 12:30 PM","reply_received_time":"09 Jul 2015 12:40 PM","time_diff":1},
                    {"dt":"09 Jul 2015","avgdelay":"30","code_sent_time":"09 Jul 2015 02:10 AM","reply_received_time":"09 Jul 2015 02:30 AM","time_diff":28},
                    {"dt":"09 Jul 2015","avgdelay":"30","code_sent_time":"09 Jul 2015 03:10 AM","reply_received_time":"09 Jul 2015 03:15 AM","time_diff":12},
                    {"dt":"09 Jul 2015","avgdelay":"30","code_sent_time":"09 Jul 2015 04:45 AM","reply_received_time":"09 Jul 2015 05:00 AM","time_diff":17},
                    {"dt":"10 Jul 2015","avgdelay":"10","code_sent_time":"10 Jul 2015 12:30 PM","reply_received_time":"10 Jul 2015 12:40 PM","time_diff":19},         
                    {"dt":"10 Jul 2015","avgdelay":"10","code_sent_time":"10 Jul 2015 02:10 AM","reply_received_time":"10 Jul 2015 02:30 AM","time_diff":21},
                    {"dt":"10 Jul 2015","avgdelay":"10","code_sent_time":"10 Jul 2015 03:10 AM","reply_received_time":"10 Jul 2015 03:15 AM","time_diff":15},
                    {"dt":"10 Jul 2015","avgdelay":"10","code_sent_time":"10 Jul 2015 04:45 AM","reply_received_time":"10 Jul 2015 05:00 AM","time_diff":15},
                    {"dt":"11 Jul 2015","avgdelay":"10", "code_sent_time":"11 Jul 2015 12:30 PM","reply_received_time":"11 Jul 2015 12:40 PM","time_diff":39},    
                    {"dt":"11 Jul 2015","avgdelay":"10", "code_sent_time":"11 Jul 2015 02:10 AM","reply_received_time":"11 Jul 2015 02:30 AM","time_diff":7},
                    {"dt":"11 Jul 2015","avgdelay":"10", "code_sent_time":"11 Jul 2015 03:10 AM","reply_received_time":"11 Jul 2015 03:15 AM","time_diff":9},
                    {"dt":"11 Jul 2015","avgdelay":"10", "code_sent_time":"11 Jul 2015 04:45 AM","reply_received_time":"11 Jul 2015 05:00 AM","time_diff":22},
                    {"dt":"12 Jul 2015","avgdelay":"10","code_sent_time":"12 Jul 2015 12:30 PM","reply_received_time":"12 Jul 2015 12:40 PM","time_diff":32},     
                    {"dt":"12 Jul 2015","avgdelay":"10","code_sent_time":"12 Jul 2015 02:10 AM","reply_received_time":"12 Jul 2015 02:30 AM","time_diff":11},
                    {"dt":"12 Jul 2015","avgdelay":"10","code_sent_time":"12 Jul 2015 03:10 AM","reply_received_time":"12 Jul 2015 03:15 AM","time_diff":52},
                    {"dt":"12 Jul 2015","avgdelay":"10","code_sent_time":"12 Jul 2015 04:45 AM","reply_received_time":"12 Jul 2015 05:00 AM","time_diff":37}
                ];

我的结果数组就像,

$scope.resarray = [     
                    {"dt":"07 Jul 2015","avgdelay":"10","data" : 
                                            [
                                            {"code_sent_time":"07 Jul 2015 12:30 PM","reply_received_time":"07 Jul 2015 12:40 PM","time_diff":10},
                                            {"code_sent_time":"07 Jul 2015 02:10 AM","reply_received_time":"07 Jul 2015 02:30 AM","time_diff":20 },
                                            {"code_sent_time":"07 Jul 2015 03:10 AM","reply_received_time":"07 Jul 2015 03:15 AM","time_diff":5 },
                                            {"code_sent_time":"07 Jul 2015 04:45 AM","reply_received_time":"07 Jul 2015 05:00 AM","time_diff":15 }
                                            ] 
                    },
                    {"dt":"08 Jul 2015","avgdelay":"20","data":
                                        [
                                            {"code_sent_time":"08 Jul 2015 12:30 PM","reply_received_time":"08 Jul 2015 12:40 PM","time_diff":35},  
                                            {"code_sent_time":"08 Jul 2015 02:10 AM","reply_received_time":"08 Jul 2015 02:30 AM","time_diff":42 },
                                            {"code_sent_time":"08 Jul 2015 03:10 AM","reply_received_time":"08 Jul 2015 03:15 AM","time_diff":5 },
                                            {"code_sent_time":"08 Jul 2015 04:45 AM","reply_received_time":"08 Jul 2015 05:00 AM","time_diff":5 }
                                        ]
                    },
                    {"dt":"09 Jul 2015","avgdelay":"30","data":
                                        [
                                            {"code_sent_time":"09 Jul 2015 12:30 PM","reply_received_time":"09 Jul 2015 12:40 PM","time_diff":1},
                                            {"code_sent_time":"09 Jul 2015 02:10 AM","reply_received_time":"09 Jul 2015 02:30 AM","time_diff":28},
                                            {"code_sent_time":"09 Jul 2015 03:10 AM","reply_received_time":"09 Jul 2015 03:15 AM","time_diff":12},
                                            {"code_sent_time":"09 Jul 2015 04:45 AM","reply_received_time":"09 Jul 2015 05:00 AM","time_diff":17},
                                        ]
                    }
                ];                      

每个日期都有4行,我想生成一个数组,其中每个日期都有另一个数组,其中包含该日期的所有4个详细信息。 我尝试了各种选择但没有成功。请帮我解决这个问题。

以下是我的尝试,

    $scope.genarr = function()
{
var data2length = $scope.data2.length;
var firstdate = $scope.data2[0].dt;
var sourcecheckdate = firstdate.split(' ');
$scope.resarray = {};
var dtcheckflag = false;
var insertrow = null;
var j = 0;
var k = 0;
var z = 1;
var data=[];
for (var i=0;i< data2length;i++)
{
    var targetcheckdate = $scope.data2[i].code_sent_time.split(' ');

    if (targetcheckdate[0] === sourcecheckdate[0] && targetcheckdate[1] === sourcecheckdate[1] && targetcheckdate[2] === sourcecheckdate[2])
    {
        firstdate = $scope.data2[i].dt;
        sourcecheckdate = firstdate.split(' ');
    }
    else
    {
        firstdate = $scope.data2[i].dt;
        sourcecheckdate = firstdate.split(' ');
    }

        if (insertrow != firstdate) 
        {
            k = 0;
            $scope.resarray[j] = {"dt":$scope.data2[i].dt,"avgdelay":$scope.data2[i].avgdelay,"data":[]};
            //$scope.resarray[j].data[k] = null;
            $scope.resarray[j].data[k]= {"code_sent_time":$scope.data2[i].code_sent_time , "reply_received_time" :$scope.data2[i].reply_received_time , "time_diff" : $scope.data2[i].time_diff};
            insertrow = firstdate;
            j+=1;
        } 
        else 
        {   
            $scope.resarray[j].data[k]=  {"code_sent_time":$scope.data2[i].code_sent_time , "reply_received_time" :$scope.data2[i].reply_received_time , "time_diff" : $scope.data2[i].time_diff};              
        }

        k +=1;
}
};

3 个答案:

答案 0 :(得分:0)

data字段当前是一个对象,但它必须是一个数组。

 {"dt":"07 Jul 2015","avgdelay":"10","data" : 
   {
     {"code_sent_time":"07 Jul 2015 12:30 PM"},
     {"code_sent_time":"07 Jul 2015 02:10 AM"},
     {"code_sent_time":"07 Jul 2015 03:10 AM"},
     {"code_sent_time":"07 Jul 2015 04:45 AM"}
   }
 },

应该是

 {"dt":"07 Jul 2015","avgdelay":"10","data" : 
   [
     {"code_sent_time":"07 Jul 2015 12:30 PM"},
     {"code_sent_time":"07 Jul 2015 02:10 AM"},
     {"code_sent_time":"07 Jul 2015 03:10 AM"},
     {"code_sent_time":"07 Jul 2015 04:45 AM"}
   ] 
 },

{}创建和对象,[]创建和数组

答案 1 :(得分:0)

var newData = [];
var dateDict = {};

angular.forEach(data2,function(d){
var temp = {};    
if(dateDict[d.dt]){
     dateDict[d.dt].push(d);
    }
else{
 dateDict[d.dt] = [];
dateDict[d.dt].push(d);
}
});

angular.forEach(dateDict,function(val,key){
 var obj = {
"dt":key,
"avgdelay":val[0].avgdelay,
"data": val
};
   newData.push(obj);
});

我认为这可以解决您的问题。 data2是旧数据,newData是您想要的数据。

答案 2 :(得分:0)

请检查:http://plnkr.co/edit/BOYqNcdpi8uGKRraDAw1?p=preview

控制器:

$scope.data = []; //New formated array of data           
for(var k = 0; k < $scope.data2.length; k++) {
  var arrayData = {}; //Temporary object
    arrayData.dt = $scope.data2[k].dt;
    arrayData.avgdelay = $scope.data2[k].avgdelay;
    arrayData.data = [];
    for(var j = 0; j<4; j++,k++) {
      if (arrayData.dt == $scope.data2[k].dt) {
      var tempObj = {'code_sent_time': $scope.data2[k].code_sent_time,
        'reply_received_time':$scope.data2[k].reply_received_time,
        'time_diff':$scope.data2[k].time_diff
      };
      arrayData.data.push(tempObj);
      }
    }  

  $scope.data.push(arrayData);  
}