I am trying to convert this JavaScript function to AngularJS, but getting few errors as I am new in AngularJS.
var array = [5, 5, 7, 9, 9, 9];
var max = array[0],
total = 0;
array.forEach((a) => {
if (a == max) {
total += max;
} else if (a > max) {
max = total = a;
}
});
console.log("total:", total);
The var array[]
is now coming from GET data and I did this in my AngularJS code.
$scope.List = getAllList;
$scope.deptime =function (){
$scope.array = $scope.List.Time;
console.log($scope.array);
$scope.max = $scope.array[0], $scope.total = 0;
angular.array.forEach((a)=>{
if(a==$scope.max){
$scope.total+=$scope.max;
}
else if(a>$scope.max){
$scope.max = $scope.total = a;
}
});
console.log("total:"+$scope.total);
};
$scope.deptime();
So I am facing this error:
TypeError: Cannot read property '0' of undefined
Where am I going wrong?
EDIT :- I am getting my response from the service in this part :- $scope.List = getAllList;