0

I am using for loop to get the units onebyone and inside loop I am getting the array of volumes like below. now I want to push that array to respective unit so I used push but here I am getting error.

My code is below

$scope.UnitDetails = [{
                       UnitId : "001"
                       Unit1 : "A"
                       Fields: [{"one" : "true","Isactive" : true },
                                 {"two" : "false","Isactive" : true }
                               ]
                      },
                       {
                       UnitId : "002"
                       Unit1 : "B"
                       Fields: [{"one" : "true","Isactive" : true },
                                 {"two" : "false","Isactive" : true }
                               ]
                      }]
     for(i= 0; i < $scope.UnitDetails.length ; i++){     
                var volume = [];                
                volume.Volume_AL = eval($scope.VolumeFormula.AL);
                volume.Volume_BL = eval($scope.VolumeFormula.BL);
                volume.Volume_CL = eval($scope.VolumeFormula.CL);
                volume.Volume_DL = eval($scope.VolumeFormula.DL);
                $scope.UnitDetails[i].push(volume);
            }

Can anyone find where i am doing mistake

EDIT

When I try as below then it is creating another array in object as below

 for(i= 0; i < $scope.UnitDetails.length ; i++){     
                    var volume = {};                
                    volume.Volume_AL = eval($scope.VolumeFormula.AL);
                    volume.Volume_BL = eval($scope.VolumeFormula.BL);
                    volume.Volume_CL = eval($scope.VolumeFormula.CL);
                    volume.Volume_DL = eval($scope.VolumeFormula.DL);
                    $scope.UnitDetails.push(volume);
                }

What I got

   $scope.UnitDetails = [{
                           UnitId : "001"
                           Unit1 : "A"
                           Fields: [{"one" : "true","Isactive" : true },
                                     {"two" : "false","Isactive" : true }
                                   ]
                          },
                           {
                           UnitId : "002"
                           Unit1 : "B"
                           Fields: [{"one" : "true","Isactive" : true },
                                     {"two" : "false","Isactive" : true }
                                   ]
                          },
                           { 
                             Volume_CL:0,
                             Volume_EQ:12,
                             Volume_PH:54,
                             Volume_RW: 24
                        }]

My Expected :

  $scope.UnitDetails = [{
                           UnitId : "001"
                           Unit1 : "A"
                           Fields: [{"one" : "true","Isactive" : true },
                                     {"two" : "false","Isactive" : true }
                                   ]
                           volume : [ { 
                             Volume_CL:0,
                             Volume_EQ:12,
                             Volume_PH:54,
                             Volume_RW: 24
                                      }]
                          },
                          {
                           UnitId : "002"
                           Unit1 : "B"
                           Fields: [{"one" : "true","Isactive" : true },
                                     {"two" : "false","Isactive" : true }
                                   ]
                          volume : [ { 
                            Volume_CL:0,
                             Volume_EQ:12,
                             Volume_PH:54,
                             Volume_RW: 24
                                     }]
                          },
                        ]
18
  • Do you by any chance mean $scope.UnitDetails[i] = volume…? Commented May 8, 2017 at 11:30
  • what is $scope.UnitDetails ? Commented May 8, 2017 at 11:30
  • 1
    Did you by any chance mean var volume = {};? Also showing the error you are getting might be useful. It seems pretty strange to me to ask on a public forum why I am getting an error and not actually showing the error that I am getting. Commented May 8, 2017 at 11:30
  • 1
    What Error are you getting. Commented May 8, 2017 at 11:31
  • 1
    Cool, but what is this $scope.UnitDetails variable? Where is it declared? How is it declared? I hope you realize that from the perspective of someone without a magic crystal ball able to read other people's minds/codes it is pretty hard to be able to help. Commented May 8, 2017 at 11:33

1 Answer 1

2

To achieve the desired result you could append the volume array dynamically to each element of $scope.UnitDetails:

for (var i = 0; i < $scope.UnitDetails.length; i++) {
    $scope.UnitDetails[i].volume = [{
        Volume_AL: eval($scope.VolumeFormula.AL),
        Volume_BL: eval($scope.VolumeFormula.BL),
        Volume_CL: eval($scope.VolumeFormula.CL),
        Volume_DL: eval($scope.VolumeFormula.DL)
    }];
}

Remark: The eval statement allows for execution of arbitrary javascript code and if the input is coming from your users you might want to ensure that it doesn't contain any malicious code before passing it to this function. Or even better do not use eval at all. Depending on your requirements you might find a more appropriate and restrictive function to achieve your goal. For example if you are expecting to evaluate only mathematical expressions written from your clients you might find a library designed exactly for this purpose rather than using the general purpose eval statement.

Sign up to request clarification or add additional context in comments.

1 Comment

How to give single line if condition to check null in that eval. like if that eval statement returns NAN or undefined then store as 0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.