简体   繁体   English

angularjs如何通过下划线或其他方法从数组中删除对象

[英]angularjs How to remove an object from a array by underscore or anything else

My question looks familiar to other questions, but it is not! 我的问题看起来很熟悉其他问题,但事实并非如此!

Please take a look to understand mine condition. 请看一下以了解地雷状况。

I have an array of objects it looks like this 我有一个看起来像这样的对象数组

$scope.events =[
    {
        $$hashKey: "009"
        _allDay:false
        _id:5
        allDay:false
        className:[]
        end:Date {Fri Aug 08 2015 12:30:00 GMT+0530 (IST)}
        start:Date {Fri Aug 08 2015 12:30:00 GMT+0530 (IST)}
        title:"Birthday Party"
    },
    {
        $$hashKey:"006"
        _id:2
        end:Date {Wed Aug 05 2015 00:00:00 GMT+0530 (IST)}
        start:Date {Sun Aug 02 2015 00:00:00 GMT+0530 (IST)}
        title:"Long Event"
    },
    {
        $$hashKey:"007"
        _id:3
        allDay:false
        id:999
        start:Date {Fri Aug 07 2015 13:00:00 GMT+0530 (IST)}
        title:"Angular Event"
    },
    {
        $$hashKey:"008"
        _id:4
        allDay:false
        id:999
        start:Date {Tue Aug 11 2015 16:00:00 GMT+0530 (IST)}
        title:"Repeating Event"
    },
    {
        $$hashKey:"00A"
        _id:6
        end:Date {Sat Aug 29 2015 00:00:00 GMT+0530 (IST)}
        start:Date {Fri Aug 28 2015 00:00:00 GMT+0530 (IST)}
        title:"Click for Google"
    }
]

Now I have to remove a object from these array, which is look like this 现在我必须从这些数组中删除一个对象,看起来像这样

var selectedObj = {
    $$hashKey:"009"
    _allDay:false
    _id:5
    allDay:false
    className:[]
    end:Date {Fri Aug 07 2015 12:30:00 GMT+0530 (IST)}
    start:Date {Fri Aug 07 2015 12:00:00 GMT+0530 (IST)}
    title:"Birthday Party"
}

What I am doing 我在做什么

removedArray = _.reject($scope.events, function(event) {
   return event.$$hashKey == selectedObj.$$hashKey
});

$scope.events = removedArray;

$scope.events is not updated I've tried $apply but not got success. $scope.events没有更新我尝试了$apply但没有成功。

could someone please help me to find out what I am doing wrong. 有人可以帮我找出我在做什么错。

What is the best practice to do. 最佳做法是什么。 This kind of dirty stuffs. 这种脏东西。

This should get you there using pretty standard javascript (IE9+): 这应该可以使用相当标准的JavaScript(IE9 +)到达那里:

var index = myArray.map(function(e) { return e.$$hashKey; }).indexOf(selectedObj.$$hashKey);

if(index != -1) {
    myArray.splice(index, 1);

With underscore.js, you can do something like: 使用underscore.js,您可以执行以下操作:

myArray = _(myArray).filter(function(obj) {
     return obj.$$hashKey!== selectedObj.$$hashKey;
});

You may want to use lodash's remove function, it is basically much the same as the answer by dustmouse, but in my opinion the code is more readable. 您可能要使用lodash的remove函数,它与尘鼠的回答基本上相同,但是在我看来,代码更易读。

var selectedObj = {...}
$scope.events = [0,1,...,n];

$scope.events = _.remove($scope.events, function(element) {
  return element == selectedObj;
});

selected event can delete by this process. 所选事件可以通过此过程删除。 call the function deleteEvent. 调用函数deleteEvent。

  $scope.deleteEvent= function() {
        var index = $scope.events.indexOf(selectedObj);
        $scope.events.splice(index, 1);

  };

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

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