Solved

Javascript array not filling properly

Posted on 2013-06-12
7
212 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…

760 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

22 Experts available now in Live!

Get 1:1 Help Now