0

I'm using a ng-change directive to get an object that change. When i get it i have to check if exists in another array of objects, and if it does i have to delete from it. what can i do? i tried with indexOf but it didn't work.

//@Param ObjectItem: change object from UI using ng-change
$scope.itemValue = function (ObjectItem) {

    //collection of required items
    var requiredItem = dataPublicationService.getRequired();

    //check if item exist in collection. DIDN'T WORK!
    if(requiredItem.indexOf(publi) !== -1){

          //get found index
          var idx = requiredItem.indexOf(publi);

      //delete founded item.
      requiredItem.splice(idx, 1);
    }
};

What other solution can i implement?

1

1 Answer 1

1

The indexOf function doesn't delete from an array, it just finds the index of the given object if found. You might consider filtering the array and testing for a match, rather than relying on indexOf to find a matching object.

Once you find your record, you need to actually alter the array using something like Array#splice.

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

1 Comment

Yes i know, the thing is to found that item in the json array to delete it after i found it. I update the code.

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.