Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

I am trying to return a value from inside a foreach loop. I would like to explain it through the code.

Below code is a function where I am looping through the data which I am fetching from the database table via $http method of angular. After looping through I need to loop again because inventory is an array so, again I run foreach loop to get only the qty. I don't know if this is good practice or a bad one but I'm still getting my hands on angular. I have written this function to check if there is a negative quantity and if there is add a class of warning to the tr as written in the code down below. What I want to learn is returning the value from inside the foreach loop. When I try to log the qty inside of the loop it logs fine but outside of it does not. I have tried searching but could not find answers to it and that is why posting my question.

$scope.invPrice = function(){
 for(var i in $scope.tableData)
 {
   $scope.pprice = $scope.tableData[i].inventory;
    angular.forEach($scope.pprice, function(index) {
     var qty = 0;
     $scope.qty = index.qty;
     qty = $scope.qty;
     console.log(qty)  //logs the qty
    });
 };
return qty;
};

<tr ng-class="{warning: invPrice() < 0}">
share|improve this question
1  
Option 1: use a normal for loop. Option 2 : define de qty outside the foreach loop . – boyd Jan 21 at 7:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.