var list = [];//大的集合
$.each(that.categoryList, function(index,value){
$.ajax({
type: 'get',
url: that.getHotUrl,
data:{
category:value.id,
pageIndex:1,
pageSize:2,
token:token
},
dataType: 'json',
success: function(response){
if(response.code==200){
var obj = new Object(); //集合对象
obj.category_name=value.name;
obj.items = response.data.items;
console.log(obj);
list.push(obj);
}
},
error: function(err) {
console.log(err);
}
});
});
console.log(list);
循环调用接口拿到数据构建一个对象数组,console结果list为空,但obj都正确
ajax是异步的
先执行的是
console.log(list);
然后在执行
console.log(obj);