I can not work out the javascript syntax to access elements of the following, which has been stringify’d after being returned as JSON from php/mysql.
vehs contains
[[{"vehID":"8","vehGrpType":"CAR","vehSubGrp":"Alfa Romeo","vehTitle":"Alfa Romeo 6C","vehPeriod":"","vehDescrip":"","vehNotes":"","vehGallery":"0","vehGallPath":"","vehGallImgs":""}],[{"vehID":"9","vehGrpType":"CAR","vehSubGrp":"Alfa Romeo","vehTitle":"Alfa Romeo 75 (1985-92)","vehPeriod":"","vehDescrip":"","vehNotes":"","vehGallery":"0","vehGallPath":"","vehGallImgs":""}],[{"vehID":"10","vehGrpType":"CAR","vehSubGrp":"Alfa Romeo","vehTitle":"Alfa Romeo GTV (1995-2000) and Spider (1995-2006)","vehPeriod":"","vehDescrip":"","vehNotes":"","vehGallery":"0","vehGallPath":"","vehGallImgs":""}]]
I am trying
for (var veh in vehs) {
$('#selectDIV').append(veh['vehTitle'] + '<br>');
}
But all I get is the numerics 0, 1 and 2, which I assume are the indexes of the three entries.
I have misunderstood something along the way, about associative arrays within array elements.
So I am a bit stuck and am not knowledgeable enough to know what combination to try next.
Any help appreciated, as I am sure it is very straightforward.
Thanks
The associative array is not an element of the main array.
The main array is composed of arrays, which only contain the associative array. Refer to it using
[0].Also, since
vehsis an array, I recommend to use a for(;;)loop to loop through it, so that user-defined prototype methods are not inherit.For performance reasons, I recommend to append the string at last.
Demo: http://jsfiddle.net/NgmFP/