2

I want to loop over an array $scope.todos

for (var i = $scope.todos.length - 1; i >= 0; i--) {
   console.log($scope.todos[i]);
};

But $scope.todos.length returns 0.

$scope.todos

looks like this:

array

What am I doing wrong? I think this is not an associative array? The indexes are numbers? It doesn't return undefined, just 0. Thanks for any help.

Edit: more code

'use strict';

angular.module('todos')

.controller('TodosController', ['$scope', '$location', '$state',      '$stateParams', 'Todos',
function($scope, $location, $state, $stateParams, Todos) {

    console.log($scope.todos);
    console.log($scope.todos.length);

    function calculateDayAygoForTodos(){
        //foreach todo in todos
            for (var i = $scope.todos.length - 1; i >= 0; i--) {
                console.log("todos length");
                    console.log($scope.todos[i]);
            };


     }

Solution: The problem was the async nature of the functions, I was trying to call the $scope.todos.length before the todos were loaded. Solved with a simple delay using timeOut function:

setTimeout(function() {
  console.log($scope.todos.length);
}, 1000);

Might not be optimal solution, but now I don't get ùndefined`anymore, but the length of the array. Thanks to War10ck for pointing this out.

RobSeg
  • 985
  • 4
  • 14
  • 37
  • 3
    When/where/how is `$scope.todos` being created/populated/loaded? – deceze Aug 03 '15 at 13:22
  • 3
    show more code, like where you log `$scope.todos`, and where you construct it – Plato Aug 03 '15 at 13:23
  • 7
    It looks like it may be a `promise` object. Promises are commonly used with async operations. It could be that your `for` loop operation is running before the action is completed and `$scope.todos` is populated... – War10ck Aug 03 '15 at 13:28
  • Seems plausible. But `console.log($scope.todos)` DOES return a full array, just the `.length` function returns 0. – RobSeg Aug 03 '15 at 13:38
  • 3
    DevTools may update the object on the console later. I bet you should see an empty array if you set a break point at `console.log($scope.todos)`. – Ghostoy Aug 03 '15 at 13:43
  • @Harbinger, if you try `console.log(JSON.stringify($scope.todos))` you can see state in stringify moment, so it will be empty array, possibly with utility promise properties – Grundy Aug 03 '15 at 13:53
  • @Harbinger also try see this [question](http://stackoverflow.com/questions/23392111/console-log-async-or-sync) – Grundy Aug 03 '15 at 14:17

2 Answers2

-1

It is possible that the length property is not set.

@steppefox (in his answer) has a good and trusted way of iterating the array.

Another way to iterate over the array is to use lodash forEach function, its similar but said to be better at performance.

Checkout lodash for other array operations as well. It has tons of it.

ArslanW
  • 353
  • 1
  • 10
-2

You can loop over array or object with:

angular.forEach($scope.todos,function(item,index){

});
steppefox
  • 1,784
  • 2
  • 14
  • 19