简体   繁体   English

如果值与其他对象匹配,如何从数组中删除对象?

[英]How to delete object from array if value matched from other object?

I have already existed array of object values now when i delete dataItem dataItem has same properties that i have in selectedOwners so if dataItem selected value matched i want to delete that object from selectedOwners array. 现在,当我删除dataItem dataItem具有与selectedOwners相同的属性时,我已经存在对象值数组,因此,如果dataItem选择的值匹配,我想从selectedOwners数组中删除该对象。

How can i achieve that task using AngularJs or Javascript ? 如何使用AngularJs或Javascript完成该任务?

ctrl.js ctrl.js

  var selectedOwners = [{"fullName":"Johnson, Rocio","workerKey":3506},{"fullName":"Johnson, John S.","workerKey":571}];

   $scope.deleteOwner = function(dataItem){
              angular.forEach(selectedOwners,function(val,index){
                if(val === dataItem){
                  selectedOwners.splice(index,1);
                }
              })
            }

Unfortunately in Javascript you don't have many instrument for a good equality checking, and === isn't enough, === don't force javascript to convert the two operand in order to perform the equality check on the same type of object, we let say that in this way you have true if the two objects have the same memory reference false otherwise. 不幸的是,在Javascript中,您没有很多工具可以进行良好的相等性检查,并且===不够,===请勿强制javascript转换两个操作数以便对相同类型的javascript进行相等性检查对象,我们可以这样说,如果两个对象具有相同的内存引用,则为true,否则为false。 For this reason you should decide the your equality criteria and wrap this login in a function. 因此,您应该确定自己的相等性标准,然后将此登录名包装在函数中。 I discourage to use something like this Object.prototype.equals because in the sway you will have the same behavior for all objects in your script 我不鼓励使用类似Object.prototype.equals类的东西,因为这样一来,您将对脚本中的所有对象都具有相同的行为

the rest of code that you had post is good in my opinion but you have implements a equality checking 我认为您发布的其余代码很好,但是您已经实现了相等性检查

I hope that this can help you 我希望这可以帮到你

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

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