简体   繁体   English

AngularJS-从JSON数组中删除搜索到的对象

[英]Angularjs - delete searched object from json array

I'm using a ng-change directive to get an object that change. 我正在使用ng-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. 我尝试使用indexOf,但是没有用。

//@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. indexOf函数不会从数组中删除,只会找到给定对象的索引(如果找到)。 You might consider filtering the array and testing for a match, rather than relying on indexOf to find a matching object. 您可能会考虑过滤数组并测试是否匹配,而不是依靠indexOf来找到匹配的对象。

Once you find your record, you need to actually alter the array using something like Array#splice . 找到记录后,您实际上需要使用Array#splice类的方法来更改数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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