Simplify multiple "else if" based on the value of a string variable

8

The following code works and has 4 conditions but I want to simplify it since they are really 150 conditions, is there any other way to reduce this algorithm and not 150 else if?

for (var j = 0; j < $scope.cants.length; j++) {
  if ($scope.cants[j].type === "bases") {       
    $scope.bases += $scope.cants[j].quantity;
  } else if ($scope.cants[j].type === "proteins") {     
    $scope.proteins += $scope.cants[j].quantity;
  } else if ($scope.cants[j].type === "veggies") {      
    $scope.veggies += $scope.cants[j].quantity;
  } else if ($scope.cants[j].type === "sauces") {       
    $scope.sauces += $scope.cants[j].quantity;
  } 
  $scope.componentsPlate = $scope.proteins +  $scope.veggies + $scope.sauces +     $scope.dryfruits + $scope.bases + $scope.proteinsExtra +        $scope.veggiesExtra + $scope.saucesExtra + $scope.dryfruitsExtra +  $scope.basesExtra
}
    
asked by Daniel Hernández 19.01.2016 в 17:49
source

3 answers

9

The syntax:

$scope.nombreDePropiedad

It is equivalent to:

$scope["nombreDePropiedad"]

Which you can take advantage of to use it in the following way:

for (var j = 0; j < $scope.cants.length; j++) {
    $scope[$scope.cants[j].type] += $scope.cants[j].quantity;
}

In this way you do not need to do any if or switch since the correct member of the $scope object will be selected from the string you pass in brackets.

    
answered by 19.01.2016 / 18:13
source
1

What you intend to do is totally contrary to what is recommended.

The objective of storing data in variables with a fixed name in a namespace is to refer to them when necessary knowing the name of the variable.

When the name assigned to the data is not known, the dictionaries are used ( arrays ) in which the name associated with the type of this is used, in case it is stored in a variable and do not know his name a priori .

What you intend to do is not only unwise, as well as unmanageable when scaling the code, but it goes against the principles of programming - what you should do is rethink how you should structure your data, to improve readability and quality of your code.

If you tell us why you intend to use variables at all costs, maybe we can help you, but without further clarification, these are my recommendations.

Later edition

Seen your problem, the solution proposed in the other answer is the best.

    
answered by 19.01.2016 в 18:07
0

Maybe you can consider the possibility of using a switch , since you mention that they are 150 casos , I think that the use of switch better the performance with respect to 150 casos , if they were a few maybe this would not be noticed but for 150 I would consider the use of the switch.

On the other hand, if you decide to use if else , I would try to place the most common cases at the beginning of if .

P.D: You say reduce the algorithm, I do not know if it also includes making changes to the efficiency of it

    
answered by 19.01.2016 в 18:32