Search code examples
javascriptangularjssplice

Splice function isn't working


here is my issue. I'm trying to remove an object from an array, but the splice function seems to provoc crash.

$http({
   method : "POST",
   url : "getDatesBasket",
   data:   {"mission":pos_id},
   async:false
}).then(function successCallback(response) {
    var rep = response['data'];
    var repSplit = rep.split(",");
    var posSplit = repSplit[0];
    var posTemp = posSplit.split(":")[1];
    for (kPos = 0; kPos < $scope.datePos.length; kPos++) {
        alert("BEFORE " +$scope.datePos[kPos].dateMax);
        if(posTemp==$scope.datePos[kPos].pos){
            alert('start suppression');
            datePos.splice(kpos,1);
            alert('end suppression');
        }
        alert("AFTER " +$scope.datePos[kPos].dateMax);
    }

when running this I see the alert boxes BEFORE and start suppression but not end suppression, it's like the splice function made the code crash.

EDIT

Here is datePos, it's an array with object built like that I perform an ajax request to get some data, then i push those data inside the tab. So this is an object with two key, pos and data

  $http({
       method: "POST",
       url: "getDatesBasket",
       data: {
           "mission": pos_id
       },
       async: false
   }).then(function successCallback(response) {
           var rep = response['data'];
           var repSplit = rep.split(",");
           var posSplit = repSplit[0];
           var dateSplit = repSplit[1];
           var posTemp = posSplit.split(":")[1];
           var date = dateSplit.split(":")[1];
           var myPos = {
               pos: posTemp,
               dateMax: date
           };
           $scope.datePos.push(myPos);    

Solution

  • You have to splice the same array which is defined on your scope.And also you are using lowercase letter inside your splice.It should be kPos .So you have to splice like this.

    $scope.datePos.splice(kPos,1);