javascript - using slice in angularjs array -


in angularjs app, need manually remove or add old/new data data array (service executed in loop). remove, use slice(); there problem: item correctly removed execverif_distant(); not executed next item. actual code, execverif_distant(); executed each item half time. example, if need remove entire array, half removed.

            // start loop, search in local datas             angular.foreach($scope.seadocument.datas.cages, function(itemlocalcages) {                 execverif_local(itemlocalcages.url);             });              function execverif_local(identifiant) {                 var itersearch_local = 0;                 angular.foreach(responsezs, function(itemdistantcages) {                     if (itemdistantcages.url == identifiant) {                         itersearch_local++;                     }                 });                 // if not find local datas in distant datas                 if (itersearch_local == 0) {                     // verifitem(); call                     verifitem('remove', identifiant);                 }             }                     // verifitem();                 function verifitem(action, url) {                     if (action == 'remove') {                         var iindex = -1;                         angular.foreach($scope.seadocument.datas.cages, function(itemlocalcages) {                             iindex++;                             if (itemlocalcages.url == url) {                                 $scope.seadocument.datas.cages.splice(iindex,1);                             }                         });                     } else {                         // nothing                     }                 } 

what's wrong ?

the problem foreach iterating on same object removing things from. avoid behavior clone object iterating before loop , work them separate:

// ... code var arrcopy = $scope.seadocument.datas.cages.slice(); //this create deep copy.  angular.foreach(arrcopy, function(itemlocalcages) {   iindex++;   if (itemlocalcages.url == url) {     $scope.seadocument.datas.cages.splice(iindex,1);   } }); //... more code 

Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -