我有一个包含如下对象的数组:
{
"date":"11/11/2014",
"time":"17.20.37",
"car":"396",
"driver":"Jenny",
"from":"Old Office",
"destination":"Log WH",
"pax":"3","comment":"",
"commenttime":"",
"arrival":"17.20.48",
"inserted":true,
"cancelled":"",
"duration":"00:00:11"
}
一旦掌握了大量数据,我希望能够根据这些数据显示统计数据。 类似的东西:
2014年11月 汽车:1,行程次数:X,道路上的时间:Y 汽车:2,行程次数:X,行驶时间:Y ...
2014年10月 汽车:4,行程次数:X,行车时间:Y 汽车:2,行程次数:X,行驶时间:Y ...
我已经能够列出这样的唯一月份对象:
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) {
for (item in monthDict[record]) {
monthDict[record][item]['month'] = moment(record.date).format('MMMM YYYY');
}
};
$scope.monthlist = monthDict;
console.log($scope.monthlist);
这会产生以下对象:
Object
November 2014: Array[5]
0: Object
arrival: "17.21.27"
cancelled: ""
car: "396"
comment: ""
commenttime: ""
date: "11/11/2014"
destination: "Gumbo Market"
driver: "Erik"
duration: "00:00:17"
from: "Luna House"
inserted: true
month: "November 2014"
pax: "3"
time: "17.21.10"
totalduration: "00:00:38"
totaldurationdriver: "00:00:17"
Object1:
Object2:
Object3:
Object4:
October 2014: Array[1]
0: Object
...
在视图中我这样显示:
<div ng-repeat="(key,val) in monthlist">
<div class="row msf-top-row">
<div class="col-md-12">
{{key}}
</div>
</div>
</div>
这已经生成了来自初始对象列表的唯一月份列表。
现在,鉴于月份列表中的每个数组都是旅行,我想对每个月/年对象内的唯一属性进行相同类型的过滤,因此我能够列出汽车(汽车:&# 34;&#34;)每个月旅行,他们花了多少次旅行,他们每个人在路上的总持续时间(总计=&#34; HH.mm.ss&#34;)。
所以基本上我想过滤已经过滤的唯一元素列表中的唯一元素。
关于如何进行的任何指针?我的大脑正在思考它......
答案 0 :(得分:0)
我会做像
这样的事情$scope.uniqueMonths = [];
angular.forEach($scope.recordlist, function(record) {
var month = moment(record.date, 'DD-MM-YYYY').format('MMMM YYYY');
if( typeof $scope.uniqueMonths[month] == 'undefined'){
$scope.uniqueMonths[month] = {
numberOfCars : 0,
numberOfTrips : 0,
timeOnTheRoad: 0,
cars : {}
};
}
/**
* Add car type into the month if does not exist
**/
if(typeof $scope.uniqueMonths[month].cars[record.car] == 'undefined'){
$scope.uniqueMonths[month].cars = {record.car : 1};
$scope.uniqueMonths[month].numberOfCars++;
}
/**
* Increment number of trips since every object implies a 'trip'
**/
$scope.uniqueMonths[month].numberOfTrips++;
/**
* Increment the time of seconds on the road.
* Needs to convert to whole value integer.
**/
$scope.uniqueMonths[month].timeOnTheRoad = $filter('secondsFmt')(record.duration);
});
在filter.js
中angular.module('myapp', []).filter('secondsFmt', function(){
return function(time) {
var totalSeconds = 0;
var times = time.split(':');
totalSeconds += parseInt(times[0]) * 3600;
totalSeconds += parseInt(times[1]) * 60;
totalSeconds += parseInt(times[2]);
return totalSeconds;
};
});
我希望这会有所帮助。如果您有疑问,请告诉我。