简体   繁体   中英

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.

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+):

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:

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.

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.

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

  };

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