2014-11-22 42 views
1

阵列我有这样看对象的数组:创建统计出与AngularJS

[ 
    { 
    "date":"20/11/2014", 
    "time":"17.54.39", 
    "car":"369", 
    "driver":"Jenny", 
    "from":"Luna House", 
    "destination":"James Hotel", 
    "pax":"3", 
    "comment":"", 
    "commenttime":"", 
    "arrival":"17.54.45", 
    "inserted":true, 
    "cancelled":"", 
    "duration":"00:00:06" 
    }, 
    { 
    "date":"23/11/2014", 
    "time":"10.54.39", 
    "car":"210", 
    "driver":"David", 
    "from":"Office", 
    "destination":"Airport", 
    "pax":"3", 
    "comment":"", 
    "commenttime":"", 
    "arrival":"11.01.45", 
    "inserted":true, 
    "cancelled":"", 
    "duration":"00:07:06" 
    } 
] 

我所试图做的是要能够提供统计出的数据的阵列。事情是这样的:

enter image description here

阵列中的每个对象都是一趟,车是“汽车:代码”和时间的道路上是一切的总和“时间:” 12:34:56" (是这样的momentJS对象)

到目前为止,我已经能够创建一个对象,使用下面的代码,列出了所有独特的几个月。

var monthDict = {}; 

angular.forEach($scope.recordlist, function(record) { 
    var month = moment(record.date, 'DD-MM-YYYY').format('MMMM YYYY'); 
    monthDict[month] = monthDict[month] || []; 
    monthDict[month].push(record); 
}); 

for (record in monthDict) {  
    var month = record;   
    for (item in monthDict[record]) { 
     monthDict[record][item]['month'] = month;   
    } 
}; 

$scope.monthlist = monthDict; 

enter image description here

的问题是,我认为,要实现什么,我需要我不得不从每月阵列中提取的独特的赛车和车手的名单,并将它推回

这就是我想我可能需要的结构:

November: { 
    Trips: [ 
     Object 1 {} 
     Object 2 {} 
    ] 
    Cars: [ 
     369 [ 
      Object 1 {} 
     ] 
     210 [ 
      Object 1 {} 
     ] 
    ] 
    Drivers: [ 
     Jenny [ 
      Object 1 {} 
      Object 2 {} 
     ], 
     Mango [ 
      Object 1 {} 
      Object 2 {} 
     ] 
    ] 
} 

因此,基本上,为每个独特的月份创建一个对象,然后在每个月份为每个独特的汽车和每个独特的驱动程序创建一个数组。

如何到达那里的任何提示?我担心现在对我来说有点过于先进。

回答

1

如果您使用lodash更容易。这将是这样做的一种方式:

HTML:

<body ng-app="myApp"> 
    <h1>Hello AngularJS!</h1> 
    <div ng-controller="myCtrl"> 
     {{hello}} 
     <pre>{{data | json}}</pre> 
     <pre>{{data2 | json}}</pre> 

     <div ng-repeat="(monthName, monthValue) in data2"> 
     <p class="month-header">{{monthName}}</p> 
     <table ng-repeat="(groupName, groupValue) in monthValue"> 
      <tr> 
      <th>{{groupName}}</th> 
      <th>Trips</th> 
      <th>Duration</th> 
      </tr> 
      <tr ng-repeat="(tripName, tripValue) in groupValue"> 
      <td>{{tripName}}</td> 
      <td>{{tripValue.trips}}</td> 
      <td>{{tripValue.duration}}</td> 
      </tr> 
     </table> 
     </div> 
    </div> 
    </body> 

JS:

var dataByMonth = _.groupBy($scope.data, function(record) { return moment(record.date, 'DD-MM-YYYY').format('MMMM YYYY'); }); 
    console.log(dataByMonth); 
    dataByMonth = _.mapValues(dataByMonth, function(month) { 
    var obj = {}; 
    obj.Cars = _.groupBy(month, 'car'); 
    obj.Drivers = _.groupBy(month, 'driver'); 

    _.each(obj, function(groupsValue, groupKey) { 
     obj[groupKey] = _.mapValues(groupsValue, function(groupValue) { 
     return _.reduce(groupValue, function(sum, trip) { 
      sum['trips']++; 
     //addDuration(sum.duration, car.duration); 
     return sum; 
     }, {trips: 0, duration: ''}) 
     }); 
    }) 

    return obj; 
    }); 
console.log(dataByMonth); 

plunker