JamesG
JamesG

Reputation: 2018

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?

Upvotes: 0

Views: 297

Answers (1)

Austin Greco
Austin Greco

Reputation: 33554

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;

Upvotes: 2

Related Questions