Solved

Javascript array not filling properly

Posted on 2013-06-12
7
216 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
[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: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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
LVL 2

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 2

Author Comment

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

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP: Filling Out/Creating a PDF 29 113
Can't get js code to execute 8 33
Put POST values into cookies. 14 35
[form-control] Retain values after a POST action 21 39
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

730 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