我有一个 AJAX 请求,要求我从服务器获取坐标值。
这是我的代码:
locationsObj = []; //Somewhere above the AJAX request
//Below is from the success callback of the request
for (var i = 0; i < rowCount; i++) {
var storelatlng = new google.maps.LatLng(
parseFloat(result.storeLat[i]),
parseFloat(result.storeLon[i])
);
locationsObj.push(??);
}
我想要的是能够将返回值存储在数组或 JSON 对象中,但我已经摆弄了很长时间,但似乎无法让它工作。我相信我可能正确存储它们,但无法将它们从数组/对象中取出,因为我对 [i] 和 [0] 感到困惑。
这是我的尝试:
locationsObj.push({storelatlng: storelatlng});
由于这是在 for 循环中,我假设它将循环并将每个 var storelatlng 添加到对象???但我如何取回这些值呢?
最佳答案
这会给你一个像这样的对象数组:
locationsObj = [
{ storelatlng: 'some value' },
{ storelatlng: 'some value' },
{ storelatlng: 'some value' }
];
因此您可以通过以下方式访问它:
locationsObj[i].storelatlng
;
关于javascript - 使用Javascript数组或JSON来存储AJAX请求的返回值以供以后使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12201084/