简体   繁体   English

使用Javascript数组或JSON存储来自AJAX请求的返回值以供以后使用

[英]Using Javascript array or JSON for storing returned values from AJAX request for later use

I have an AJAX request to bring me the values of coordinates from the server. 我有一个AJAX请求,要求我从服务器获取坐标值。

Heres my code: 继承我的代码:

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(??);


                    }

What I want is to be able to store the returned values in an array OR JSON object, but Ive been fiddling around for ages and I cant seem to get it to work. 我想要的是能够将返回的值存储在数组或JSON对象中,但我已经摆弄了很多年,我似乎无法让它工作。 I believe I may be storing them right, but can not get them out of the array/object as I am getting confused with the [i]'s and [0]'s. 我相信我可能正确地存储它们,但是当我与[i]和[0]混淆时,它们无法将它们从数组/对象中取出。

Here is my attempt: 这是我的尝试:

locationsObj.push({storelatlng: storelatlng});

As this is in the for loop, I assume it will loop through and add each var storelatlng to the object??? 由于这是在for循环中,我假设它将循环并将每个var storelatlng添加到对象??? But how do I get the values back out? 但是如何让价值回归?

this will give you an array of objects like this: 这会给你一个像这样的对象数组:

locationsObj = [
  { storelatlng: 'some value' },
  { storelatlng: 'some value' },
  { storelatlng: 'some value' }
];

so you would access it by: 所以你可以访问它:

locationsObj[i].storelatlng ; locationsObj[i].storelatlng ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM