Solved

Javascript array not filling properly

Posted on 2013-06-12
7
213 Views
Last Modified: 2013-06-12
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?
0
Comment
Question by:VieleFragen
  • 4
  • 3
7 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 39241437
Two dimensional arrays in js are formatted like so

arrayname[x][y]

Not
arrayname[x,y]
0
 
LVL 1

Author Comment

by:VieleFragen
ID: 39241619
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).
0
 
LVL 58

Accepted Solution

by:
Gary earned 150 total points
ID: 39241710
var gaAllStations = new Array();
..
...
    for (var iintStationLoop = 0; iintStationLoop < gcStations.length; iintStationLoop ++) {
       sCurrentBand = gcStations[iintStationLoop].parentNode.parentNode.getAttribute("id");
       gaAllStations[iintStationLoop]= new Array()
       gaAllStations[iintStationLoop][gciNODE_NAME] = sCurrentBand;
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:VieleFragen
ID: 39241813
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?
0
 
LVL 58

Expert Comment

by:Gary
ID: 39241820
That's it.
0
 
LVL 1

Author Comment

by:VieleFragen
ID: 39242001
Thanks very much for the information and your time; I appreciate it.
0
 
LVL 1

Author Closing Comment

by:VieleFragen
ID: 39242006
I appreciate that the experts at Experts Exchange have time and patience for new learners.  Thanks EE!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Angular 2 - Issue in page Layout 2 46
Full Screen problem when auto scale IFRAME 2 55
Change text to radio button and calendar form 2 32
Message not shown 5 29
I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now