AngularJS - ForEach Accessing the previous / next index array, perhaps?

$scope.todos = [ {text:'learn', done:true}, {text:'build', done:false}, {text:'watch', done:false}, {text:'destroy', done:false}, {text:'rebuild', done:false}]; $scope.remaining = function() { var count = 0; angular.forEach($scope.todos, function(todo,i) { todo.text = 'want2learn'; todo.text[i+1] = todo.text; }); 

Is it possible for the ForEach function to access / set the value of the previous or next object index value, is it possible?

+4
source share
1 answer

Yes,

 $scope.todos[i+1].text = todo.text; 

You want to protect against indexing after the end of the array, though:

 if(i < $scope.todos.length - 1) { $scope.todos[i+1].text = todo.text; } 
+5
source

All Articles