Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS - ForEach Accessing previous/next index array, Possible?

Tags:

angularjs

  $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 a ForEach function accessing/set previous/next index array value of object, Possible?

like image 498
vontdeux Avatar asked Feb 07 '13 18:02

vontdeux


1 Answers

Yes,

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

You'll want to guard against indexing past the end of the array though:

if(i < $scope.todos.length - 1) {
   $scope.todos[i+1].text = todo.text;
}
like image 155
Mark Rajcok Avatar answered Oct 03 '22 00:10

Mark Rajcok