views:

41

answers:

2

Hi guys.

I need some help regarding jquery arrays.


var queryArr;

$(markersArray).each(function(index) {

     var _locationId = index;
     var _locName    = markersArray[index].name;
     var _markerLat  = markersArray[index].marker.getLatLng().lat();
     var _markerLng  = markersArray[index].marker.getLatLng().lng();

//I DO NOT KNOW THE SYNTAX BELOW HELP IN HERE PLEASE..

      var locations = {  

        locationId:_locationId;                                
        locationName:_locName,
        lat:_markerLat,
    lng:_markerLng  }
    queryStr = { "locations": locations}    //??????????

      });


queryArr.push(location); //???????

}

I need to reach each element by using sth like this:

alert(queryArr[0].locations.locationId);

Obviously I will be using a for loop to reach each data by an index.

Can anyone give me example syntax for this.. I have found plenty examples of fixed arrays but not dynamic content.

+1  A: 

You're very close. The line with push needs to be moved inside the .each() loop and you need to use queryStr for the push. Make sure you don't have any other syntax errors like missing semicolons.

queryArr.push(queryStr);

Here's your example:

var queryArr = [];

$(markersArray).each(function(index) {
     var _locationId = index;
     var _locName    = markersArray[index].name;
     var _markerLat  = markersArray[index].marker.getLatLng().lat();
     var _markerLng  = markersArray[index].marker.getLatLng().lng();

     var locations = {  
        "locationId" :_locationId;                                
        "locationName" :_locName,
        "lat" :_markerLat,
        "lng" :_markerLng  
     };
     queryStr = { "locations" : locations };
     queryArr.push(queryStr);
 });
rchern
and that line needs to go inside the "each" function
cambraca
@cambraca, Whoops, missed that in the weird indenting/formatting. Will edit, thanks.
rchern
not really, it still does not work. I know syntax is not correct that is why I am asking an example that will show a syntax format. I have edited the } in the question. it was just a copy paste mistake. but thanks for warning. any more help :)
Ozlem
@user485941 Edited to include your code.
rchern
thanks guys...especially rchern. I add var queryArr=[]; and corrected the queryArr.push(queryStr); mistake.. and it works now..million thanks.
Ozlem
A: 
Ozlem