1

Simple question I hope (not sure what I am missing);

How can I push items to a basic and not an indexed array?

When doing this:

var shops = [];     
angular.forEach(data, function(value, index) { //loop over data
    shops.push(value.SHOP_CODE);
});

enter image description here

4
  • 1
    It is the same thing Commented Dec 3, 2016 at 6:50
  • 2
    Those are the same thing. Note you might have logged that array before it was filled (which is why you got Array[0] as it had no length at the time. But when you went to go look at it in the console it had filled. The initial line for a logged array does not update when the array updates Commented Dec 3, 2016 at 6:50
  • I think you're correct. Im returning a promise and it's likely not being returned fast enough. Racking my brain - Thank you. Commented Dec 3, 2016 at 6:52
  • 1
    JavaScript arrays always use numbered indexes. Commented Dec 3, 2016 at 6:54

2 Answers 2

1

When you are logging the shops array to console, the array is probably empty and hence it says Array[0].

The contents of the array are evaluated again when you click the dropdown arrow beside Array[0]. So at the time of your click on the dropdown the array is not empty and hence showing 4 elements.

If the array were not empty at the point where you do console.log(shops), then you would get the result you are looking for.

I suggest you add a breakpoint at the statement console.log(shops) and check the contents of the array.

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

1 Comment

Thank you @shriharska. Actually it was that I wasn't waiting for the promise to return and the array wasn't filling completely.
1

Thanks to @Patrick Evans and @SLePort, I realized I needed to wait for the promise. This works:

.$promise.then(function(data) {
   $scope.workOrder = data;
       angular.forEach(data, function(value, index) { //loop over aircraft data
                        shops.push(value.SHOP_CODE);
                });
           shops.sort();
           console.log(shops);
          });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.