jas jas - 5 months ago 11
Javascript Question

how to create multi-dimensional array in jquery

I'm trying to populate data for markers on map. I can get it to work using static content but I would like to set sites content by reading from an array.

var sites = [
['User 1', 45.4210328, -75.6900218, 4],
['User 2', 40.315939, -105.440630, 2],
['User 3', 43.785890, -101.90175, 1],
['User 4', 39.99948, -105.28370, 3]
];

setMarkers(map, sites);


I tried this


for (var i = 0, len = arr.length; i < len; i++)
{
var sites = [
[arr[i].username, arr[i].latitude, arr[i].longitude, i
];
}


But after this, sites only contain one array i.e last array. Not all the arrays. How can I create multi-dimensional array within sites?

Answer

Try this:

var sites = [];
for (var i = 0, len = arr.length; i < len; i++)
{     
   sites.push([arr[i].username, arr[i].latitude, arr[i].longitude, i]);
}
Comments