简体   繁体   中英

Error on acessing second row of two dimensional javascript array

I have a two dimensional javascript array which needs to get it's first column data ([j][0]) from the entries of a one dimensional array containing street addresses.

A combined string is taken from the DOM which PHP inserts in a previous function. The string then is split into a one dimensional array which contains all the street addresses I want to show on Google Maps. The two dimensional array named "markers" is to be used by Google Maps javascript API.

The array contains as many rows as the street addresses and three columns: the first for the street address, the second for the latitude and the the third for the longitude.

So here is my problem: Since Google Maps isn't showing any map markers on the map, I am trying to debug the code by adding alert("markers[j][0]: " + markers[j][0]); in the for loop in order to view each row of the loop (each street address).

The first entry (markers[0][0]) shows correctly but the second entry (markers[1][0] is not alerted to the browser at all. I suspect the the error/problem is after alert("j: " + j); in the loop because the rest of the javascript program is not run at all after this loop. (I have some more alerts after this loop and some code to render the map but it isn't run).

Here is my code which contains the error. Please help because I'm trying to fix this issue for two days now and it's driving me nuts.

if(document.getElementById('address-target'))
{
 var div = document.getElementById("address-target");

 LocationAddressesCombined = div.textContent;

 alert("Location addresses combined: " + LocationAddressesCombined);
}


var LocationAddresses = LocationAddressesCombined.split(',');

alert("LocationAddresses[0]: " + LocationAddresses[0]);
alert("LocationAddresses[1]: " + LocationAddresses[1]);



var MarkersColumns = ["",0,0];
var markers  = [[MarkersColumns]];


alert("LocationAdresses.length" + LocationAddresses.length);

var j;

//Loop through the "markers" array entries and assign to them the location addresses.
for (j=0; j < LocationAddresses.length; j++)
{
  alert("j: " + j);

  markers[j][0] = LocationAddresses[j];

  alert("markers[j][0]: " + markers[j][0]);

}

Your markers[j] has to be an array before you put something into the 0th element of it. Try this:

markers[j] = [];
markers[j][0] = LocationAddresses[j];

By the way, I'm not sure what you intended to accomplish with these lines:

var MarkersColumns = ["",0,0];
var markers  = [[MarkersColumns]];

...but that actually sets up a 3-D array where markers[0][0][0] is "" , markers[0][0][1] is 0 , and markers[0][0][2] is 0 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM