0

I have an array with empty rows,I have the field "codeOperation" to test if a row is empty or not,this is my table:

enter image description here

The client should fill the table whith data and left the rest of rows empty, what I want is when the client click on the "ADD" boutton only the data will be send and the emoty rows will be deleted.

this is my code:

//function to send the Data
$scope.updateGamme= function(gamme) {

    gamme.listElementGammeOf = $scope.finalOperationsList;

    $scope.DeleteEmptyRows(gamme.listElementGammeOf);

             $http
                 .put(
                     baseUrl +
                     "/gamme/update",
                     gamme)
                 .success(
                     function(gammeModifiee) {
                         //send the Data and update 
                            .....     
                     }); }

//delete the empty rows
$scope.DeleteEmptyRows = function(listelements){
            for (var i = 0; i < listelements.length; i++) {
                if (listelements[i].operationCode == "")
                    listelements.splice(i, 1);
            }

What I get as a result with this code, is that per example I get 5 items, my code will remove the rows 3 and 4 the row 2 is not deleted

Is there any problem with my code? Please help me find it.

Thanks for help

5
  • 1
    Could you make a JSFiddle with your runnable code? Commented Jun 7, 2016 at 9:50
  • 1
    if you use 'use strict' you can just check if !listelements[i].operationCode, it would then show false for undefined, null and empty strings, if you do not have the 'use strict' statement, you will have to check for undefined, null or empty string Commented Jun 7, 2016 at 9:51
  • sorry Sir I can't this code is simplified,I can't put all the code in JSFiddle Commented Jun 7, 2016 at 9:56
  • 2
    Bad idea to change the object you are iterating over. you might wanna just add a check in Add Button to check if there is an empty element already present and then return instead of processing at submit Commented Jun 7, 2016 at 10:13
  • ok thanks Sir for your note Commented Jun 7, 2016 at 10:26

2 Answers 2

3

Looks like

for (var i = 0; i < listelements.length; i++) {
    if (listelements[i].operationCode == "")
        listelements.splice(i, 1);
}

should be

for (var i = 0; i < listelements.length; i++) {
    if (listelements[i].operationCode == "")
        listelements.splice(i--, 1);
}

When you iterate and remove items from an array, you should decrement your index not to miss an item after the index shift due to removing.

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

Comments

2

Try splicing in reverse order. i.e remove rows from the last one.
I haven't tried your code but it must work.

 $scope.DeleteEmptyRows = function(listelements){
        for (var i = listelements.length-1; i >=0; i--) {
            if (listelements[i].operationCode == "") {
                listelements.splice(i, 1);
                         }
                   }
  }

The example I tried is...

var array = ["1","2","","",""];  
for(var i=array.length-1;i>=0;i--)
{
    if(array[i]=="")
       array.splice(i,1);
}

Comments

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.