javascript - can't push to array Objects -
i have problem array objects. have variable "setting". if write:
var settings=[]; var tempsettings=[{ id:1, name:"test1" }, { id:2, name:"test2" } ]; settings=tempsettings; console.log(settings[0]);
all right-all work. settings[0]- no problem;
but if received info file , do:
jquery.getjson("myurl", function(data) { console.log(data); var zones=data.split("~"); jquery.each(zones, function(key, value) { var set = value.split(","); var tset={ id:set[0], name:set[1] }; settings.push(tset); }); }); console.log(settings[0]);
this not work settings[0] - undefined. mistake?
data received , console.log(data); me string data.
added:
console.log(tempsettings) in variant hardcodded does:
[object { id=1, name="test1"},object { id=2, name="test2"} ]
and console.log(settings) in received variables does: [].
but in after click in console see:
[0] object { id="3", name="test3"}, [1] object { id="4", name="test4"}.
getjson
asynchronous. need include console.log
within callback function:
jquery.getjson("myurl", function(data) { console.log(data); var zones=data.split("~"); jquery.each(zones, function(key, value) { var set = value.split(","); var tset = { id:set[0], name:set[1] }; settings.push(tset); }); console.log(settings[0]); });
javascript arrays object
No comments:
Post a Comment