简体   繁体   中英

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.

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. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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