从下面的代码示例中,我试图在遍历 text_array 的每个用户时从文本中获取位置。从文本中提取位置后,我试图将值放回与正确用户对应的数组中,但它给了我错误“text_array [i] 未定义”。我做错了什么?
function replace_undefined(text_array) {
var userLocationText = {};
for (i = 0, l = text_array.length; i < l; i++) {
console.log(text_array[i].user);
userLocationText[text_array[i].user] = text_array[i].location;
var text = text_array[i].text;
Placemaker.getPlaces(text, function (o) {
console.log(o);
if ($.isArray(o.match)) {
if (o.match[0].place.name == "Europe" || o.match[0].place.name == "United States") {
var location = o.match[1].place.name;
userLocationText[text_array[i].user] = location;
}
if ($.isArray(o.match)) {
if (o.match[0].place.name !== "Europe") {
var location = o.match[0].place.name;
userLocationText[text_array[i].user] = location;
}
}
} else if (!$.isArray(o.match)) {
var location = o.match.place.name;
userLocationText[text_array[i].user] = location;
}
console.log(text_array);
});
}
}
}
text_array = [{
user: a,
user_id: b,
date: c,
profile_img: d,
text: e,
contentString: f,
url: g,
location: undefined
}, {
user: a,
user_id: b,
date: c,
profile_img: d,
text: e,
contentString: f,
url: g,
location: undefined
}, {
user: a,
user_id: b,
date: c,
profile_img: d,
text: e,
contentString: f,
url: g,
location: undefined
}];