?
Solved

Javascript array not filling properly

Posted on 2013-06-12
7
Medium Priority
?
218 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:Alan Varga
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2

Author Comment

by:Alan Varga
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 600 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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
LVL 2

Author Comment

by:Alan Varga
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 2

Author Comment

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

Author Closing Comment

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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…
Suggested Courses

777 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