0

I have a hard coded global array that looks like

   //var sectionL=[1,5,3,7,7,4,5,3,4,3,6,6,4,5,4,1];

Instead I would like to fill that global array from JSON request I tried with

var sectionL=[];

 function fillsectionL(){
 $.getJSON(baseURL+'&callback=?',function(data ){
 var len= Object.keys(data['result']['sectionL']).length;
    for(var i =0;i<len;i++){
      sectionL[i]=data['result']['sectionL'];
    }
});

}

I read about asynchronous and that the output will be fired before the code is executed. Is there a simple way to do something like

 sectionL=fillSection(); 

and have fillSection return an array?

3 Answers 3

2

I suggest to follow this link to learn how to work with javascript arrays.

http://www.w3schools.com/jsref/jsref_push.asp

0

The "getJSON" api you're using here (and indeed, all JS browser-based APIs for interacting with remote servers) is asynchronous. It happens "in the background" and calls your callback function when it's finished fetching data. If you have code that needs to run after this callback completes, you can try simply calling that code from within your callback, as the least complicated way to get started.

0
sectionL[i]=data['result']['sectionL'];

You can use Push method to fill array like that

sectionL.push(data['result']['sectionL']);


I don't know json format you received so i amused your received correct  

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.