javascript - can't push object into array -


this question has answer here:

i'm missing basic. i'm grabbing data api , trying push array of json objects. api works fine , i'm getting data, not seem pushed array. can't seem console log of data parameters. here jquery code:

    var jsonarr =[]     var jsondata = {}        $.get(apigetlist,function(r) {         $(r).each(function(i) {             $.get(api_checklist_start + r[i].id +api_checklist_end ,function(s){               $(s[0].checkitems).each(function(k){                jsondata =                   {"item": [k],                     "task": r[i].name,                     "name": s[0].checkitems[k].name,                     "state": s[0].checkitems[k].state                   }     jsonarr.push(jsondata)               })             })           })         })  console.log(jsonarr) 

when view console see:

enter image description here

the array empty, of objects api coming through. if try console.log jsonarr[0] undefined.

any ideas on how access objects have pulled in api?

your $.get method asynchronous. therefore console.log() runs before method returns anything.

try console.log() after finished pushing elements.


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -