javascript - Trouble referencing JSON imported object outside of FOR -
I am having trouble referencing my imported JSON data. If I run the loop, then I get == 1 and the output works if I try to refer it outside of the four loop, then it fails with the error of "Uncort Type Error": the property Can not read 'Tname' undefined "
This works ...
For (var i) in cities {if (i == 1) { Ctx.fillText (towns [i] .name, 0, 0);}}
This fails with the next line "Uncort Type Error": 'undefined' Tname 'property Do not read This additional line is written directly after the loop above.
ctx.fillText (Towns [1] .Tname, 0, 0);
< P> I have tried many formats but all are unsuccessful. "Towns [1]" is the production "[object object]". For those people, this is a snippet from the city. Jason: {"Tid": "2057277", "Tname": "York"}, {"Tid": "2057575", "Tname": "Yanchep" }}}
This is JSON for the cities [code]:
var cities []; $ .getJSON ('towns.json', function (Data) {for data i.towns var i) {city [I] = data.towns [i]}});
Thank you for your response This was a case of syntax for inside and outside of the loop Thanks for pointing me to the different @Bargi in the right direction.
// This loop worked inside ctx.fillText (Towns [i] .name, 0, 0); // same syntax loop ctx.fillText (failed outside towns [1] .name, 0, 0); // This syntax loop worked out ctx.fillText (Towns [1] ['Tname'], 0, 0);
Comments
Post a Comment