0

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.

0

3 Answers 3

3

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;
});
Sign up to request clarification or add additional context in comments.

Comments

1

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;
});

Comments

0

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);

  };

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.