I have an arrangement in which each item has a status, for what I do is look for the GENERATED or PENDING states and depending on each search a number is assigned to the variable value and then used in a switch and depending on that value do certain operations.
Here's my code:
var myApp = angular.module('myApp',[]);
//myApp.directive('myDirective', function() {});
//myApp.factory('myService', function() {});
function MyCtrl($scope) {
$scope.name = [
{"id":1, "estado":"NO_AUTORIZADO"},
{"id":2, "estado":"PENDIENTE"},
{"id":3, "estado":"NO_AUTORIZADO"},
{"id":4, "estado":"AUTORIZADO"},
{"id":5, "estado":"AUTORIZADO"},
{"id":6, "estado":"NO_AUTORIZADO"},
{"id":7, "estado":"AUTORIZADO"},
{"id":8, "estado":"PENDIENTE"},
{"id":9, "estado":"NO_AUTORIZADO"},
{"id":10, "estado":"AUTORIZADO"}
];
var valor = 0;
angular.forEach($scope.name, function(item){
if(item.estado === 'GENERADO'){
valor = 1;
}else{
if(item.estado === 'PENDIENTE'){
valor = 2;
}else{
valor = 3;
}
}
})
switch(valor){
case 1:
console.log(valor);
//hacer operaciones correspondientes
break;
case 2:
console.log(valor)
//hacer operaciones correspondientes
break;
case 3:
console.log(valor+' no hay nada que hacer')
break;
default:
return false;
}
}
But my mistake is that he always gets the last value. How can I solve this problem? ... the idea is that I want to do it outside the For cycle.
in advance I thank you ....!