Javascript数组为空不为空(Ajax)
我正在尝试检索API google发送给我的地址到一个数组中。问题是我刚收到一封信 包含所有my元素的空数组(地址)。 因为我的第一印象,我尝试了异步:伪造我的ajax 异步是问题所在,但它什么也没做。 如果你有什么想法,那就好了,谢谢 输出:Javascript数组为空不为空(Ajax),javascript,php,ajax,symfony,Javascript,Php,Ajax,Symfony,我正在尝试检索API google发送给我的地址到一个数组中。问题是我刚收到一封信 包含所有my元素的空数组(地址)。 因为我的第一印象,我尝试了异步:伪造我的ajax 异步是问题所在,但它什么也没做。 如果你有什么想法,那就好了,谢谢 输出: (4) [Array(0), Array(0), Array(0), Array(0)] 0: Array(0) id: 9 coord: "Rue des Haies 56, 6001 Charleroi, Be
(4) [Array(0), Array(0), Array(0), Array(0)]
0: Array(0)
id: 9
coord: "Rue des Haies 56, 6001 Charleroi, Belgique"
length: 0
__proto__: Array(0)
1: [id: 10, coord: "43 Rue de Boulainvilliers, 75016 Paris, France"]
2: [id: 11, coord: "Grand Place 22, 7000 Mons, Belgique"]
3: [id: 12, coord: "28 Place Sébastopol, 59000 Lille, France"]
length: 4
__proto__: Array(0)
我的代码:
let geocoder = new google.maps.Geocoder;
$.ajax({
type: "POST",
url: "{{ path('url') }}",
async : false,
success: function (data) {
let positions = JSON.parse(data);
let allAddress = [];
Array.from(positions).map((position, index) => {
allAddress[index] = [];
let latlng = {
lat: parseFloat(position['latitude']),
lng: parseFloat(position['longitude'])
};
let idPosition = position['id'];
geocoder.geocode({'location': latlng}, function (results, status) {
if (status === google.maps.GeocoderStatus.OK) {
let searchCoords = results[0]['formatted_address'];
setTimeout(function() {
allAddress[index]['id'] = idPosition;
allAddress[index]['coord'] = searchCoords;
}, 0);
} else {
console.log("Geocode wasn't successful for the following reason : " + status);
}
});
});
console.log(allAddress);
}
});
您需要使用Promises和Promise all,因为地理编码器是异步的。下面是成功块
let positions = JSON.parse(data);
let allAddress = [];
// array to hold promises
const geoPromises = []
Array.from(positions).map((position, index) => {
allAddress[index] = [];
let latlng = {
lat: parseFloat(position['latitude']),
lng: parseFloat(position['longitude'])
};
let idPosition = position['id'];
// push the promise into our array
geoPromises.push(new Promise(function(resolve, reject) {
geocoder.geocode({
'location': latlng
}, function(results, status) {
if (status === google.maps.GeocoderStatus.OK) {
let searchCoords = results[0]['formatted_address'];
allAddress[index] = {
id: idPosition,
coord: searchCoords,
};
// resolve the promise
resolve(results)
} else {
console.log("Geocode wasn't successful for the following reason : " + status);
// reject it
reject(status)
}
});
}))
});
// wait for all the promises to complete
Promise.all(geoPromises).then(function(values) {
// show your addresses
console.log(allAddress);
}).catch(error => {
console.error(error.message)
});
因为geocoder.geocode是异步的,所以不知道为什么会有setTimeout。我想填充数组,但如果数组是异步的,我不能这样做,所以我尝试将填充的每个元素都设置为setTimeout。我不知道这是否足够清楚…setTimeout在异步添加时不会起任何作用。问题是你正在循环所有内容并进行所有调用,而不是等待所有调用完成。你需要用promise all查看承诺。好的,我将阅读文档并尝试这样做。我有两分钟的时间,所以写下了要做的事情我尝试了你的解决方案,但我总是有相同的答案空数组不是空的:(````(4)[数组(0),数组(0),数组(0),数组(0)]0:数组(0)id:9坐标:“比利时沙勒罗街566001号”长度:0原型:阵列(0)1:[id:10,坐标:“法国巴黎75016号布兰维尔街43号”]2:[id:11,坐标:“比利时蒙斯大广场22号,7000号”]3:[id:12,坐标:“法国里尔59000里尔塞巴斯托波尔28号”]长度:4原型:阵列(0)``我将一个阵列(0)分为阵列和阵列(0)不是空的,但长度是空的我编辑了里面你添加属性的部分谢谢,你是对的。我还有很多东西要学:)