5

Hi it seams like i can't push my array ? code:

  $scope.arrResult = [];
  dpd.timesheets.get( function (result) {
    console.log(result);

    for (i = 0, n = result.length; i < n; i++) {
      var item = result[i];
      $scope.arrResult[item.week].push(item);
    }
    console.log($scope.arrResult);

  });

i get this console error

Uncaught TypeError: Cannot read property 'push' of undefined

if i set $scope.arrResult[item.week].push(item); to $scope.arrResult[item.week] = item; it works with no error but i need / want to push, whats wrong ?

0

1 Answer 1

12

This is because

$scope.arrResult[item.week]

itself is not an array.

push() is of Array.prototype

To see what I mean, try

$scope.arrResult[item.week] = [];

or

$scope.arrResult[item.week] = new Array();

and then try push()

Sign up to request clarification or add additional context in comments.

13 Comments

I see, mmm.. but how do i do this the correct way then, if i set = [] before push, i just get one object in the array, and i want all :)
@Stweet, what do you mean 'all'. Maybe don't even need an array?
result contaions alot of objects, and i want this objects sortet in $scope.arrResult[week] eg. $scope.arrResult[21] contains all objects of week 21
@Stweet So, if item is an array, then do $scope.arrResult[item.week].concat(item);
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.