2014-11-14 86 views
0

我有一个与看起来像这样的对象数组:过滤从独特的结果,并在AngularJS已筛选列表

{ 
"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 ...

月 汽车: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> 

这已经产生的从最初的对象列表来的唯一几个月的列表。现在,考虑到monthlist中的每个数组都是一次旅行,我想对每个月/年对象内的独特属性进行相同类型的过滤,因此我可以列出汽车(car:“”)每个月旅行的次数,他们乘坐的次数,每个人的道路总持续时间(totalduration =“HH.mm.ss”)。

所以基本上我想过滤已经过滤的独特元素列表中的独特元素。

任何关于如何进行的指针?我的大脑是纺纱想到它..

回答

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; 
    }; 
}); 

我希望这有助于。如果您有任何问题,请告诉我。

+0

只需一条评论:我不需要显示汽车数量,但是需要显示对象中每个特定汽车代码的统计数据(例如:“car”:“396”)。关于这个时间,对象中的所有时间属性都是momentJS对象,所以我可以轻松地操作它们。无论如何,明天我会尝试一下你的代码,如果第一部分工作正常,也许我可以自己找出其余的部分。谢谢!! – 2014-11-14 18:08:09

+0

如果您想通过“car”:“396”进行过滤,请将$ scope.uniqueMonths [month]更改为$ scope.uniqueMonth [car],并通过执行未定义类型检查来使月份唯一。 – tsuz 2014-11-14 18:13:23