My goal with this code is to populate a global array with JSON objects.
// declaring global arrays
var sectionL=[1,5,3,7,5,4,3,3,4,4,6,6,3,5,5,1];
var sectionsTitle=new Array(16);
var sectionsContent=new Array(16);
for(var i=0; i<sectionL.length;i++){
sectionsTitle[i]=new Array(sectionL[i]);
sectionsContent[i]=new Array(sectionL[i]);
}
var country = new Array(4);
for(var i=0;i<5;i++){
country[i]=new Array();
country[i][2]=sectionsTitle;// assigning as shown above
country[i][3]=sectionsContent;// assigning as shown above
//////////////////////////////////////
function getCountries(data){
$.each(data['countries'], function (k,val){
country[k][0]=val['title']; // storing the title
country[k][1]=val['id']; // storing the id for JSON
$.getJSON(baseURL+country[k][1]+'&callback=?',function(data ){
console.log("k=>"+k+" id=>"+country[k][1]); // checking if k is passed correctly
for(var i=0;i<1;i++){
index=i+1;
for(var j=0;j<1;j++){
country[k][2][i][j]=data['result']['sectionTitles']['section'+index+'.'+j+'.title']; // filling the array with content
}
}
});
});
console.log(country);
output:
[Array[4], Array[4], Array[4], Array[4], Array[4]]
k=>0 id=>9
k=>1 id=>29
k=>3 id=>31
k=>4 id=>12
k=>2 id=>7
Issue: The arrays are being overwritten, displaying only the last value. I suspect it's a closure problem, but I'm unsure how to proceed. Any suggestions would be helpful!