Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot set property '0' of 2D array

Can anyone tell me why I'm getting this error for the code below:

Uncaught TypeError: Cannot set property '0' of undefined

var vehicles = [];
$.get('../poll/index.php?data=vehicles', function(data) {

    var rows = $(data).find('row').length;
    for (var i = 0; i < rows; i++) {
        vehicles[i][0] = $(data).find('row').eq(i).find('stage').text();
        vehicles[i][1] = $(data).find('row').eq(i).find('direction').text();
        vehicles[i][2] = $(data).find('row').eq(i).find('stageName').text();
        vehicles[i][3] = $(data).find('row').eq(i).find('atco').text();
        vehicles[i][4] = $(data).find('row').eq(i).find('service').text();
        vehicles[i][5] = $(data).find('row').eq(i).find('journey').text();
        vehicles[i][6] = $(data).find('row').eq(i).find('fleet').text();
        vehicles[i][7] = $(data).find('row').eq(i).find('longitude').text();
        vehicles[i][8] = $(data).find('row').eq(i).find('latitude').text();
        vehicles[i][9] = $(data).find('row').eq(i).find('operator').text();
        vehicles[i][10] = $(data).find('row').eq(i).find('position').text();
    }

}, 'xml');
like image 576
jskidd3 Avatar asked Nov 29 '22 01:11

jskidd3


1 Answers

You need to define each child array, e.g.

var vehicles = []; // parent array;
vehicles[0] = []; // first child array;

so you would need:

for (var i = 0; i < rows; i++) {
     var vehicles[i] = [];
     ... rest of code here ...
}
like image 157
Marc B Avatar answered Dec 15 '22 09:12

Marc B