Ionic 3 compute average

#1

Hi, I want to compute average from API value.

my html

<ion-row *ngFor="let playerHistorie of playerHistories" class="table-padding row" nowrap=""> 
   <ion-col col-2 class="text-center">
      <div>{{ careerMin(playerHistorie)  }}</div>
    </ion-col>
</ion-row>

my .ts

  careerMin(playerHistorie) {
   // let a = playerHistorie.sMinutesAverage;
    
    let sum = 0; 
    for(var i = 0; i < playerHistorie.length; i++){
        sum += parseInt(playerHistorie.sMinutesAverage[i], 10); //don't forget to add the base 
    }

    let avg = sum/playerHistorie.length;

    return avg; 

  }

my output is “NaN”

#2

Are you certain that each of those parseInt calls are returning proper numbers?

Incidentally, never do what you are doing here: calling heavy functions from inside template expressions. Do this once, store the result in a controller property, and reference that property in the template. Angular change detection will call this function a lot, which is very wasteful.