I am facing an issue with reading data from a JSON file. Here is the code snippet from my controller:
myApp.controller("abcdctrl", ['$scope', 'orderByFilter', '$http', function ($scope, orderBy, $http) {
console.log('abcdctrl');
$http.get("http://localhost:8080/api/session")
.then(function (response) {
$scope.data = response.data.session;
});
$scope.getAvg = function () {
var total = Number("0");
for (var i = 0; i < $scope.data.length; i++) {
total += parseInt($scope.data[i].testing);
}
return parseInt(total / $scope.data.length);
}
}]);
Here is the JSON data that I am working with:
{
"session": [
{
"id": 1,
"testing": "91,92,93,94,95,96,97",
"playing": "11,12,13,14,15,16,17",
"acc_id": 1
},
{
"id": 2,
"testing": "101,102,103,104,105,106,107",
"playing": "1,2,3,4,5,6,7",
"player_id": 2
},
{
"id": 3,
"testing": "111,112,113,114,115,116,117",
"playing": "21,22,23,24,25,26,27",
"acc_id": 3
}
]
}
I am trying to calculate the average value of each player for testing and playing, as well as the total average value for testing and playing. While I have been able to print the entire JSON successfully, I am encountering difficulties in accessing specific elements within the JSON structure.
Your assistance on this matter would be greatly appreciated. Thank you!