Link to home
Start Free TrialLog in
Avatar of Alan Varga
Alan VargaFlag for United States of America

asked on

Javascript array not filling properly

I am trying to fill a two-dimensional array, but the first dimension appears to be not incrementing correctly.  Here is a partial code listing:

outside the function, expecting to be globally available for reading and writing
    var gciNODE_NAME = 0;

    // use either one of these, but not both; one must be commented out or deleted altogether
    var gaAllStations = new Array();
    var gaAllStations = [];

Open in new window


inside the function, filling the array
    for (var iintStationLoop = 0; iintStationLoop < gcStations.length; iintStationLoop ++) {
        sCurrentBand = gcStations[iintStationLoop].parentNode.parentNode.getAttribute("id");
        gaAllStations[iintStationLoop, gciNODE_NAME] = sCurrentBand;
...

Open in new window


Even though iintStationLoop is incremented to 1 in the loop counter, it statys stuck on 0 in gaAllStations[iintStationLoop, gciNODE_NAME], thereby overwriting the original value.  When iintStationLoop = 14, I end up with sCurrentBand in gaAllStations[0, gciNODE_NAME], not in gaAllStations[14, gciNODE_NAME].

What is happening here?
Avatar of Gary
Gary
Flag of Ireland image

Two dimensional arrays in js are formatted like so

arrayname[x][y]

Not
arrayname[x,y]
Avatar of Alan Varga

ASKER

I tried changing my code to this:

    for (var iintStationLoop = 0; iintStationLoop < gcStations.length; iintStationLoop ++) {
        sCurrentBand = gcStations[iintStationLoop].parentNode.parentNode.getAttribute("id");
        gaAllStations[iintStationLoop][gciNODE_NAME] = sCurrentBand;

Open in new window


but now I get "TypeError: can't convert undefined to object"

I also tried changing my array declaration to:

var gaAllStations = [][];

Open in new window


but then I get "ReferenceError: getRadioData is not defined" (the function that fills the array).
ASKER CERTIFIED SOLUTION
Avatar of Gary
Gary
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That did the trick.  So what you did was declare a global, one-dimensional array, and then inside the loop you created a "second" array inside the first one.  Am I understanding this correctly?
That's it.
Thanks very much for the information and your time; I appreciate it.
I appreciate that the experts at Experts Exchange have time and patience for new learners.  Thanks EE!