javascript - can't push object into array -
this question has answer here:
- how return response asynchronous call? 21 answers
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:
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
Post a Comment