0

I setup a global variable to hold the data array:

var data = [];

This is a global variable as it is used outside of a function and needs to be accessible for all functions

I then iterate over a data set and populate the data array.

When I access this array using the console:

console.log(window.data);

I can see the following in Chromes console view:

enter image description here

There are many more records in the data set, they all follow exactly the same format

enter image description here

The issue I am having is, I cannot access this data array and am seeing zero length as shown below:.

console.log(window.data.length); // returns 0

I'm not clear on why this is occurring and am hoping someone can point out what I am doing wrong.

It seems like the result of processing this array is not available at the time I am trying to get the length, therefore I am getting zero.

Have tried the following at the end of the html file:

    $( document ).ready(function() {
        console.log("ready!");        // returns ready!
        console.log(window.data[15]); // returns undefined
    });

Not quite sure how to proceed, so any advice would be very welcome.

EDIT:

This was resolved by the use of promises (bottom link in the duplicate notice above). Previous code was removed as it didn't add to the discussion and would probably only create confusion

Core issue was based on data not being available on page load due to processing of large arrays.

Majickal
  • 176
  • 2
  • 16
  • where do you assign values to data[]? – Nidhin Joseph Jul 10 '19 at 01:43
  • ... and **when**? – Phil Jul 10 '19 at 01:44
  • @NidhinJoseph see updated question – Majickal Jul 10 '19 at 01:50
  • @Phil I see you marked this as duplicate, however I am not trying to change the behavior of the console, so I am a little confused as to how this question is a duplicate of the one you linked to. – Majickal Jul 10 '19 at 01:55
  • The duplicate explains what you're seeing. Try some of the answers out to get a clearer picture of the state of your program. I also highly recommend using your browser's debugger instead of the console for actual debugging – Phil Jul 10 '19 at 01:59
  • @Phil I understand what you mean and adding a watch to the data var returns `data: ` on page load. The issue I think I am having is, when the code is executed the array is not yet populated and therefore not immediately available as it's still processing. When I refresh just the watch, the data is there :) I think this is perhaps a different issue to what is linked in the duplicate notice you added. That is why the critical bit of information in the original post was that it is available in the console, but not as a variable on page load. – Majickal Jul 10 '19 at 02:11
  • 1
    @Majickal I added another link that should help – Phil Jul 10 '19 at 02:12

0 Answers0