I have a simple ng-repeat that displays a list of values (financial income). How can i calculate the average from this list?
<div ng-repeat="data in MyData">
{{ data.income }}
</div>
<div>
Average:
</div>
Which displays:
11
14
8
9
21
10
2
1
5
13
Thanks
In HTML
Average: {{calculateAverage(MyData)}}
Code
$scope.calculateAverage = function(MyData){
var sum = 0;
for(var i = 0; i < MyData.length; i++){
sum += parseInt(MyData[i], 10); //don't forget to add the base
}
var avg = sum/MyData.length;
return avg;
};