[英]Angularjs - delete searched object from json array
我正在使用ng-change指令来获取更改的对象。 当我得到它时,我必须检查是否存在于另一个对象数组中,如果存在,则必须从中删除。 我能做什么? 我尝试使用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);
}
};
我还可以实施什么其他解决方案?
indexOf
函数不会从数组中删除,只会找到给定对象的索引(如果找到)。 您可能会考虑过滤数组并测试是否匹配,而不是依靠indexOf
来找到匹配的对象。
找到记录后,您实际上需要使用Array#splice
类的方法来更改数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.