Alan Varga
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
inside the function, filling the array
Even though iintStationLoop is incremented to 1 in the loop counter, it statys stuck on 0 in gaAllStations[iintStationL oop, 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?
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 = [];
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;
...
Even though iintStationLoop is incremented to 1 in the loop counter, it statys stuck on 0 in gaAllStations[iintStationL
What is happening here?
ASKER
I tried changing my code to this:
but now I get "TypeError: can't convert undefined to object"
I also tried changing my array declaration to:
but then I get "ReferenceError: getRadioData is not defined" (the function that fills the array).
for (var iintStationLoop = 0; iintStationLoop < gcStations.length; iintStationLoop ++) {
sCurrentBand = gcStations[iintStationLoop].parentNode.parentNode.getAttribute("id");
gaAllStations[iintStationLoop][gciNODE_NAME] = sCurrentBand;
but now I get "TypeError: can't convert undefined to object"
I also tried changing my array declaration to:
var gaAllStations = [][];
but then I get "ReferenceError: getRadioData is not defined" (the function that fills the array).
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
ASKER
Thanks very much for the information and your time; I appreciate it.
ASKER
I appreciate that the experts at Experts Exchange have time and patience for new learners. Thanks EE!
arrayname[x][y]
Not
arrayname[x,y]