That's because you're setting the local variable taskToUpdate
to an empty object, meanwhile $scope.tasks
still retains the original reference to the unaltered object.
Once you have the reference to the object you can then get the indexOf
.
var index = _.indexOf($scope.tasks, taskToUpdate);
And then can blank out the object reference in the $scope.tasks
$scope.tasks[index] = {};
At this point the reference in $scope.tasks
will be a blank plain object, but tasksToUpdate
will continue to hold the object reference. You can choose to continue to use it at this point, or not. But once you've ended all closures that have a reference to that taskToUpdate
object, it will cease to exist.
My guess is that you actually want to remove the reference from $scope.tasks
rather than just blanking out the object reference. You can do this many ways, but here is one:
$scope.tasks = _.without($scope.tasks, taskToUpdate);