无法访问javascript对象的属性
问题描述:
我有一个问题来访问一个javascript对象的属性。 我有一个ajax调用,我创建一个对象,并在完整的一部分,我想使用创建的对象,但我无法访问属性,当我控制台登录ojbect它在那里,但不能访问属性 这是我的代码:无法访问javascript对象的属性
这里面的Ajax调用 治疗我有谁在AJAX完成创建一个对象
var repartion = getEventsByCity(villes,date);
我即使.done尝试的功能:
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']);
答
检查repartion
存在,而你打印repartion['75001']
。
console.log(repartion && repartion['75001']);
随着代码,如果是repartion
存在,它将打印repartion [ '75001']。你可以尝试一下。
而且你可以离开我的评论,
+0
我试过仍未定义 –
尝试'的console.log(repartion [75001]);' – Iceman
你尝试这样'的console.log(repartion [75001])的整数键;'? – dloeda
我猜这有[与Ajax调用有关](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call)。你必须展示[mcve]。 – JJJ