0

EDIT: here is a full gist of my code (API key of course removed): https://gist.github.com/tconroy/e52e0e7402face8f048e

I am having some issues with accessing objects{} stored inside a JavaScript array[].

When I do console.dir(containerArray), I can see the 2 objects stored inside the array, like in the photo below:

enter image description here

However, when I perform

console.log(containerArray),

the output is just empty brackets [].

When I try and access the array indexes via console.dir(containerArray[0]);, the output is undefined.

This is the code I am using to create the objects and put them into the array:

var key  = String(decodeURIComponent(addr)),
obj  = {};
obj[key] = json.data.weather;
containerArray.push(obj);

What am I doing wrong here? I need to be able to access each of the objects stored in the array and not sure what I'm doing wrong here.

tdc
  • 5,174
  • 12
  • 53
  • 102
  • Pls put the code & output here, not as picture – toesslab May 07 '14 at 17:19
  • 1
    I have a suspicion, but please show how you are creating this `containerArray`. – Niet the Dark Absol May 07 '14 at 17:19
  • I have added the code for how I am creating the objects. containerArray is just created like this: `var containerArray = [];` – tdc May 07 '14 at 17:21
  • possible duplicate of [Is Chrome's JavaScript console lazy about evaluating arrays?](http://stackoverflow.com/questions/4057440/is-chromes-javascript-console-lazy-about-evaluating-arrays) – Bergi May 07 '14 at 18:20

1 Answers1

1

Judging by the json.data object, I'm going to take a shot in the dark.

You are loading containerArray's data in some kind of AJAX callback, but trying to access it outside of said callback.

Example:

MyAJAXFunction("url", function(json) {
    containerArray.push(json.data.weather);
});
alert(containerArray[0]); // why undefined?

The reason it seems to work with console.dir can be seen by hovering over that little i icon: It has a reference to the object, which was updated before you browsed it.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • yes this is exactly what I'm doing -- how do I pass the actual object into the array instead of just a reference? Does the json.data.weather no longer exist outside of the callback, even though I'm pushing it to array? – tdc May 07 '14 at 17:27
  • Anything that uses the result of a callback, MUST itself be in the callback, called by the callback, or otherwise deferred until after the callback has completed. – Niet the Dark Absol May 07 '14 at 17:29
  • So I cannot retrieve the data in the callback, store it in a global var, and access it outside of the callback? All of my processing code needs to exist inside the callback? That seems really messy and tedious :/ – tdc May 07 '14 at 17:31
  • my issue is I'm actually looping through inputs, doing AJAX call with each input, and I want to store the JSON results for formatting into a graph in a later function, so I want to send all the data at once so the graph is created at the same time with all needed data. – tdc May 07 '14 at 17:54
  • "called by the callback" may be your best option. Call `dataReady()` or something to do your actual code. – Niet the Dark Absol May 07 '14 at 17:54
  • yes but how do I make sure I have data from all my ajax calls before calling `dataReady()`? I don't want to perform actions on each response individually, but rather as a group. – tdc May 07 '14 at 18:09