使用jquery创建动态数组

发布于 2024-09-29 01:47:09 字数 851 浏览 1 评论 0原文

我需要一些关于 jquery 数组的帮助。


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

显然

我需要通过使用这样的方式来访问每个元素:

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

,我将使用 for 循环通过索引来访问每个数据。

任何人都可以给我示例语法吗?我发现了很多固定数组的示例,但没有找到动态内容。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冧九 2024-10-06 01:47:09

你们非常接近。带有推送的行需要移动到 .each() 循环内,并且您需要使用 queryStr 进行推送。确保没有任何其他语法错误,例如缺少分号。

queryArr.push(queryStr);

这是您的示例:

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);
 });

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);
 });
提赋 2024-10-06 01:47:09
var elements = document.getElementsByTagName("div"); // returns a nodeList
var array = jQuery.makeArray(elements );
$(array ).appendTo(document.body);

或者喜欢

var object = $('li');
var array = $.makeArray(object);
var elements = document.getElementsByTagName("div"); // returns a nodeList
var array = jQuery.makeArray(elements );
$(array ).appendTo(document.body);

or like

var object = $('li');
var array = $.makeArray(object);
挽梦忆笙歌 2024-10-06 01:47:09
var queryArr=[];
for ( var index=0; index<markersArray.length; 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);

}

//test - works:)

for ( var i=0; i<markersArray.length; i++){ 

alert(
 queryArr[i].locations.locationId+"--"+
 queryArr[i].locations.locationName +"--"+
 queryArr[i].locations.lat);

}
var queryArr=[];
for ( var index=0; index<markersArray.length; 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);

}

//test - works:)

for ( var i=0; i<markersArray.length; i++){ 

alert(
 queryArr[i].locations.locationId+"--"+
 queryArr[i].locations.locationName +"--"+
 queryArr[i].locations.lat);

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文