I have problem with array Objects. I have variable "setting". If I 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 I received data from file and 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. What my mistake?
Data I received and console.log(data); get 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 I see:
[0] Object { id="3", name="Test3"}, [1] Object { id="4", name="Test4"}.
~does not have anything to iterate over. I would inspect what's indatato make sure it has something you expect.datapassed to the callback of.getJSONshould be an object, not a string.});from yourgetJSONin that code btw.console.log(set[0]);andconsole.log(set[1]);output? also, jQuery.getJSON is not closed properly