can';无法访问javascript对象的属性
我在访问javascript对象的属性时遇到问题。 我有一个ajax调用,我在其中创建了一个对象,在完整的部分中,我想使用创建的对象,但当我控制台记录ojbect时,我无法访问属性,因为它在那里,但无法访问属性 这是我的代码: 这是ajax调用中的内部处理 我有一个创建对象的函数can';无法访问javascript对象的属性,javascript,jquery,ajax,Javascript,Jquery,Ajax,我在访问javascript对象的属性时遇到问题。 我有一个ajax调用,我在其中创建了一个对象,在完整的部分中,我想使用创建的对象,但当我控制台记录ojbect时,我无法访问属性,因为它在那里,但无法访问属性 这是我的代码: 这是ajax调用中的内部处理 我有一个创建对象的函数 var repartion = getEventsByCity(villes,date); 在ajax complete中,我甚至尝试使用。完成: console.log(repartion) 它打印 Objec
var repartion = getEventsByCity(villes,date);
在ajax complete中,我甚至尝试使用。完成:
console.log(repartion)
它打印
Object {}
75001: Array(4)
0: Object
1: Object
2: Object
3: Object
length: 4
__proto__: Array(0)
__proto__: Object
因此,对象是定义的,但如果在我尝试之后
console.log(repartion['75001']);
它打印未定义
如果我尝试
$.each(repartion, function(index,value){
console.log(index + " " + value);
});
它不打印任何东西。。。
我不明白,有人能帮我找出我做错了什么我不知道
谢谢
更新:
我会显示更多的代码
这是我的ajax电话
function callApi(data){
$.ajax({
url: 'events_finder.js',
data: data,
success: function(arg){ console.log('success ');},
complete: function(arg){
console.log('complete ');
// this one working and print the object
console.log(repartion);
// not showing anything
$.each(repartion, function(index,value){
console.dir(index + " " + value);
});
// showing undefined
console.log(repartion['75001']);
},
error: function(xhr,status,erreur){ console.log(xhr.status+" "+status+" "+ erreur);},
}).done(function(){
console.log("DONE");
// this one working and print the object
console.log(repartion);
// not showing anything
$.each(repartion, function(index,value){
console.dir(index + " " + value);
});
// showing undefined
console.log(repartion['75001']);
})
}
这是events_finder.js
var villes = data['villes'];
var date = data['date'];
// i put only one city for the debug
villes = ['75001']
function getEventsByCity (villes,date){
var repartion_events = {};
for(i = 0 ; i < villes.length ; i++){
var oArgs = {
app_key: "fzgree4546Rx" ,
where: "France, "+villes[i],
date: date,
sort_order: "popularity",
page_size: "400",
};
//console.log(oArgs);
EVDB.API.call("/events/search", oArgs, function(oData) {
//console.log(oData);
// console.log(typeof oData['events']['event']);
if(oData['events'] != null){
if (oData['events']['event'] != null){
var kikou = oData['events']['event'];
//console.log(kikou);
var eventsByCity = new Array;
$.each(kikou, function( index, value ) {
var events = {
"description": kikou[index]['description'],
"image": kikou[index]['image'],
"latitude": kikou[index]['latitude'],
"longitude": kikou[index]['longitude'],
"postal_code": kikou[index]['postal_code'],
"start_time": kikou[index]['start_time'],
"stop_time": kikou[index]['stop_time'],
"title": kikou[index]['title'],
"url": kikou[index]['url'],
"venue_address": kikou[index]['venue_address'],
"venue_name": kikou[index]['venue_name'],
"venue_url": kikou[index]['venue_url']
};
// rajouter l'objet dans le tableau de la ville
eventsByCity.push(events);
ville = kikou[index]['postal_code'];
});
// console.log(eventsByCity);
// console.log(ville);
repartion_events[ville] = eventsByCity;
}
}
});
}
return repartion_events;
}
var repartion = getEventsByCity(villes,date);
// print the object this working
console.log(repartion repartion['75001'] );
// this one show undefined
console.log(repartion['75001']);
var-villes=data['villes'];
var日期=数据[‘日期’];
//我只为调试设置了一个城市
villes=['75001']
函数getEventsByCity(villes,date){
var-repartion_-events={};
对于(i=0;i
tryconsole.log(重新分区[75001])
您是否尝试过这样的整数键console.log(重新分区[75001])代码>?我猜这已经发生了。您必须显示。是的,我尝试重新分区[75001]不工作,甚至尝试将对象更改为数组,并尝试重新分区[0]或循环不工作,但仍未定义…您是否从AJAX的done
函数中访问数组。