Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
83
rated 0 times [  87] [ 4]  / answers: 1 / hits: 21400  / 11 Years ago, sun, june 2, 2013, 12:00:00
$scope.clearCompleted = function()
{
angular.forEach($scope.todos, function(todo, i)
{
if(todo.done)
{
$scope.todos.splice(i, 1);
}
});

if($scope.todos.length == 0)
{
$scope.isEmpty = true;
}
}


This is my code to delete the 'done' todos from an array,
but when two todos after each other are removed, it only removes the second.
I think it's because the splice function resets and the returns the spliced array.


More From » angularjs

 Answers
4

You splice elements from the array, which you iterated, therefore indexes in todos reduced. Sorry for my bad english.



var notDonedTodos = [];
angular.forEach($scope.todos, function(todo, i)
{
if(!todo.done)
{
notDonedTodos.push(todo);
}
});

$scope.todos = notDonedTodos;

[#77865] Friday, May 31, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
leilanichasel

Total Points: 224
Total Questions: 112
Total Answers: 94

Location: Angola
Member since Wed, Apr 13, 2022
2 Years ago
;