简体   繁体   中英

Angularjs - delete searched object from json array

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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM