3

I am trying to get the length of an array which has objects in. I am using the following test method to loop through the array using the length. But for some reason the length is 0, even though it has 47 Items in it, see screenshot.

Code -

public test() {
    console.log(this._metadataModel.Currency);
    for (var i = 0; i <= this._metadataModel.Currency.length; i++) {
        console.log(i);
    }
}

The array

Screen of console.log(array)

From console I get--

Empty array, yet has items

Empty Brain
  • 627
  • 8
  • 24
Darian
  • 45
  • 1
  • 6
  • 1
    How is that array generated? – Luca Kiebel Sep 19 '18 at 09:56
  • 5
    Probably unrelated, but you have an off-by-one error. You should loop until `i < array.length`, not `<=` – Federico klez Culloca Sep 19 '18 at 09:56
  • `console.log(this._metadataModel.Currency.length);` whats the result – Beginner Sep 19 '18 at 09:58
  • 1
    I have added the result as a screenshot. – Darian Sep 19 '18 at 09:59
  • 1
    Note the console's view of an object (and array) are updated when you expand them in the console. So what you see was a view of it at that time, and not a view of it at the time of the call. Do a `console.log(JSON.stringify(this._metadataModel.Currency))` to see exactly what the array is at the time of the for loop – Patrick Evans Sep 19 '18 at 09:59
  • I think the array is populated dynamically from a back-end service and then parsed into my Angular2+ UI – Darian Sep 19 '18 at 09:59
  • 1
    When you post screenshots, please give context. The second screenshot just shows an empty array, but it's not clear how you obtained that output. – Federico klez Culloca Sep 19 '18 at 10:01
  • 2
    My first guess is the array is populated asynchronously, and since the console is live, it will update. However at the time of the for loop, the Array is still empty. –  Sep 19 '18 at 10:01
  • @Darian the bottom line is you need to wait for the array to populate before using its data. See https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call –  Sep 19 '18 at 10:05
  • I delete my answer make sure that you have an array not a json object – Osama Sep 19 '18 at 10:16
  • 1
    It looks to me like you're trying to log the contents of the array, before your load completed - aka "load meta data completed" is logged after your for. Try to make sure you're running the test method, once your data has loaded. – bug-a-lot Sep 19 '18 at 10:46

1 Answers1

0
for ( var item of this._metadataModel.Currency ) {
    console.log(item);
}

Try this.

Then from item you can access item.id, item.value, etc.

dcangulo
  • 1,888
  • 1
  • 16
  • 48
  • This is not the issue; the problem is that the array is indeed empty when the `for` loop runs. –  Sep 19 '18 at 10:05
  • @ChrisG I see. My bad. But I guess this method eliminates the purpose of getting the length and be able to iterate the object. Unless he got something he wants to do with the index. – dcangulo Sep 19 '18 at 10:11