0

I'm trying to read stats out of an array of objects that looks like 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 is a test",
    "commenttime":"19.11",
    "arrival":"19.12",
    "inserted":true
}

I'm using the Unique module from AngularUI to be able to make a list of all drivers or all cars, which so far works OK and creates the following table:

table of stats

<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 the 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 the road</div>
    </div>
  </div>
</div>

Every object is a trip. My problem right now is that I want to be able to both count how many objects contain each of the unique record.car or record.driver properties (to be able to determine how many trips a car took), and also to make operations with momentJS to be able to determine how much time a particular car or driver was on the road (timeOnRoad = record.time - record.arrival).

I'm a bit lost on whether this is even possible to do.

Any input?

ANSWER

The answer from Ilan worked perfectly! Here's my code after I adapted it slightly.

var carsDict = {};

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

And the HTML:

 <div class="col-md-5">
        <div class="row" ng-repeat="carcode in carstats">
          <div class="col-md-4">{{carcode[0].car}}</div> <!-- Car code -->
          <div class="col-md-4">{{carcode.length}}</div> <!-- Nº of objects (trips) inside carcode -->
          <div class="col-md-4">Time on the road</div>
        </div>
 </div>
3
  • Try adding a column in ng-repeat {{$index}} Commented Nov 8, 2014 at 18:02
  • @Linial can you expand on that? Thanks! Commented Nov 8, 2014 at 18:10
  • I didn't realize that you need to perform actions on those lists. anyway, in any ng-repeat scope you get those extra variables $index, $first, $last and so on. $index variable is simply referring to the index of the specific ng-repeat. Commented Nov 8, 2014 at 18:13

1 Answer 1

3

First create a dictionary of cars to 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 you can make all calculations for each car.

Sign up to request clarification or add additional context in comments.

3 Comments

I have a list of cars, but the cars can be added or removed, and I want to be able to also read stats about cars that are no longer on the list, therefore I'm reading from the records list and not from the cars list.
@EricMitjans I only touched the records list
I got your code to work (See updated question)! Many thanks for that! The only problem is that I don't really understand your function. Care to explain? Thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.