I want to get the average rating from the Reviews and display it but I'm unsure on how to do it.
JSON
"Reviews":[
{
"OwnerID":1,
"Comment": "Charlotte is brilliant. A lovely person. Thank you!",
"rating":4
},
{
"OwnerID":2,
"Comment": "The best dog carer ever! She is amazing",
"rating":4
}
],
html file
<ion-item ng-repeat="item in PetCareProfile>
<span ng-repeat="rate in item.Reviews" class="item-text-wrap"> {{calculateAverage(rate.rating)}} </span>
</ion-item>
Controller
.controller('CareTakerProfileCtrl',
function($scope,$http,$stateParams,$ionicPopup, ionicDatePicker)
{
$http.get('js/SampleJson.json').success(function (data) {
$scope.PetCareProfile = data.PetCareTakers;
$scope.whichPetCareProfile = $stateParams.aId;
$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;
};
})
Result: NaN NaN NaN
What I wanted: 4.3333333