javascript - Counting objects that contain specific property in AngularJS -



javascript - Counting objects that contain specific property in AngularJS -

i'm trying read stats out of array of objects looks this:

{ "time":"19.09", "car":"429", "driver":"julia", "from":"james hotel", "destination":"juba teaching hospital", "pax":"2", "arrival":"19.09", "inserted":true } { "date":"25/10/2014", "time":"19.11", "car":"396", "driver":"tom", "from":"drilling company", "destination":"james hotel", "pax":"2", "comment":"this comment test", "commenttime":"19.11", "arrival":"19.12", "inserted":true }

i'm using unique module angularui able create list of drivers or cars, far works ok , creates next table:

<div class="row msf-stats-data-row" > <div class="col-md-5"> <div class="row" ng-repeat="record in recordlist | unique:'car'"> <div class="col-md-4">{{record.car}}</div> <div class="col-md-4">trips</div> <div class="col-md-4">time on road</div> </div> </div> <div class="col-md-6 pull-right"> <div class="row" ng-repeat="record in recordlist | unique:'driver'"> <div class="col-md-6">{{record.driver}}</div> <div class="col-md-2">trips</div> <div class="col-md-4">time on road</div> </div> </div> </div>

every object trip. problem right want able both count how many objects contain each of unique record.car or record.driver properties (to able determine how many trips auto took), , create operations momentjs able determine how much time particular auto or driver on road (timeonroad = record.time - record.arrival).

i'm bit lost on whether possible do.

any input?

answer

the reply ilan worked perfectly! here's code after adapted slightly.

var carsdict = {}; angular.foreach($scope.recordlist, function(record) { carsdict[record.car] = carsdict[record.car] || []; carsdict[record.car].push(record); }); $scope.carstats = carsdict;

and html:

<div class="col-md-5"> <div class="row" ng-repeat="carcode in carstats"> <div class="col-md-4">{{carcode[0].car}}</div> <!-- auto code --> <div class="col-md-4">{{carcode.length}}</div> <!-- nÂș of objects (trips) within carcode --> <div class="col-md-4">time on road</div> </div> </div>

first create dictionary of cars store references of records per car.

var carsdict = {}; angular.foreach(recordlist, function(record) { carsdict[record.car] = carsdict[record.car] || []; carsdict[record.car].push(record); });

then can create calculations each car.

javascript angularjs

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -