0

我正在尝试使用 Firebug API 填充二维数组,

var sites = [];
    var siteCounter = 0;

    //Firebase API Calls

    var messageListRef = new Firebase('https://my.firebaseio.com');
    messageListRef.once('value', function(allMessagesSnapshot) {
      allMessagesSnapshot.forEach(function(messageSnapshot) {
        var latitude = messageSnapshot.child('latitude').val();
        var longitude = messageSnapshot.child('longitude').val();
        sites[siteCounter][0] = 'siteCounter';
        sites[siteCounter][1] = latitude;
        sites[siteCounter][2] = longitude;
        sites[siteCounter][3] = siteCounter;
        sites[siteCounter][4] = 'This is siteCounter.';

        siteCounter++;
        alert(sites[siteCounter][1]  + " " + sites[siteCounter][2] );
      });
    });

但这打破了网站[siteCounter][0] 并说它是未定义的。任何线索如何使用这个?

4

2 回答 2

0

sites[siteCounter] = []; 以前试过sites[siteCounter][0]吗?

我假设您必须将其声明为数组才能将内容放入数组中。

于 2013-09-29T11:09:39.897 回答
0

如果你想要二维数组,你必须像这样初始化:

var tab = new Array(); 

tab[0] = new Array();

试试这个代码

sites[siteCounter] = new Array(); 
sites[siteCounter][0] = 'siteCounter';
sites[siteCounter][1] = latitude;
sites[siteCounter][2] = longitude;
sites[siteCounter][3] = siteCounter;
sites[siteCounter][4] = 'This is siteCounter.';

或直接

sites[siteCounter] = new Array("siteCounter", latitude, longitude, siteCounter, "This is siteCounter."); 
于 2013-09-29T11:10:27.057 回答