我遇到一个问题,我无法找到它的真正根本原因,我猜它是一个可变范围问题,但我不确定。
所以,这是我的代码:
beerslist = { thebeers : []};
beerworld = { countries : []};
$.post("sample.php", function(data){
data = JSON.parse(data);
//alert(data.items);
$.each(data.items, function(i, one){
beerslist.thebeers.push({
id : one.beer.bid,
name: one.beer.beer_name,
style : one.beer.beer_style,
abv : one.beer.beer_abv,
ibu : one.beer.beer_ibu,
rating : one.beer.rating_score,
my_rating : one.beer.auth_rating,
brewery : one.brewery.brewery_name,
country : one.brewery.country_name,
state : one.brewery.location.brewery_state,
});
//$("#beerlist").append("<div>" + one.beer.bid + "" + one.beer.beer_name + " " + one.beer.rating_score + " " + one.beer.auth_rating + " " + one.brewery.brewery_name + " " + one.beer.beer_style + " " + one.beer.beer_abv + " " + one.beer.beer_ibu + " " + one.brewery.country_name + "</div>");
});
});
alert(JSON.stringify(beerslist));
$.each(beerslist.thebeers, function(i,beer){
var flag = false;
$.each(beerworld.countries, function(j,country){
if (country.name == beer.country){
flag = j;
}
});
if (flag == false){
beerworld.countries.push({
code : "AA",
name : beer.country,
state : beer.state,
rating : beer.my_rating,
ammount : 1,
});
} else {
beerworld.countries[flag].ammount = beerworld.countries[flag].ammount + 1;
beerworld.countries[flag].rating = (beerworld.countries[flag].rating + beer.my_rating)/2;
}
});
alert(JSON.stringify(beerworld));
alert(JSON.stringify(beerslist));
当我触发第一个警报时,它会显示一个空对象,其中只有声明值“{”thebeers“:[]}”。所以我进入下一步,在那里我迭代创建第二个json对象并且数组正常工作,生成“啤酒世界”显然没有问题。我在这个secon $ .each节点中插入警报,所有计算都在那里。但是当我走出循环时,结果对象全部搞砸了,没有我使用“ beerworld.countries [flag] .rating ”警告看到的值,例如,验证。 哦,在最后一个$ .each之后,我也可以毫无问题地访问对象啤酒列表。
我感谢任何帮助!
感谢。
答案 0 :(得分:2)
Jquery Ajax
是异步的,这就是为什么在完成ajax请求之前你的下一个代码正在执行的原因。这可以通过回调来解决。请参阅以下代码:
beerslist = {
thebeers: []
};
beerworld = {
countries: []
};
$.post("sample.php", function(data) {
data = JSON.parse(data);
//alert(data.items);
$.each(data.items, function(i, one) {
beerslist.thebeers.push({
id: one.beer.bid,
name: one.beer.beer_name,
style: one.beer.beer_style,
abv: one.beer.beer_abv,
ibu: one.beer.beer_ibu,
rating: one.beer.rating_score,
my_rating: one.beer.auth_rating,
brewery: one.brewery.brewery_name,
country: one.brewery.country_name,
state: one.brewery.location.brewery_state,
});
//$("#beerlist").append("<div>" + one.beer.bid + "" + one.beer.beer_name + " " + one.beer.rating_score + " " + one.beer.auth_rating + " " + one.brewery.brewery_name + " " + one.beer.beer_style + " " + one.beer.beer_abv + " " + one.beer.beer_ibu + " " + one.brewery.country_name + "</div>");
});
processResponseCallback(); //calling callback when your request is finish
});
function processResponseCallback() {
alert(JSON.stringify(beerslist));
$.each(beerslist.thebeers, function(i, beer) {
var flag = false;
$.each(beerworld.countries, function(j, country) {
if (country.name == beer.country) {
flag = j;
}
});
if (flag == false) {
beerworld.countries.push({
code: "AA",
name: beer.country,
state: beer.state,
rating: beer.my_rating,
ammount: 1,
});
} else {
beerworld.countries[flag].ammount = beerworld.countries[flag].ammount + 1;
beerworld.countries[flag].rating = (beerworld.countries[flag].rating + beer.my_rating) / 2;
}
});
alert(JSON.stringify(beerworld));
alert(JSON.stringify(beerslist));
}