2014-11-14 81 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