I am working with Leaflet for a map on my site. I want to build the layer switcher dynamically. The constructor takes an object, with key being name of the layer to display on map and value being the layer object itself. I am building the switcher dynamically from an array returned by AJAX. The problem I'm having is that I don't know how to get the value of an array object to be the key in my new object. My code looks like this:
$.ajax ({
url: '...',
data: { ... },
dataType: 'json',
success: function (data) {
var overlayMaps = {};
for (var i in data.elems) {
var layer = new Layer (...);
overlayMaps = $.extend ({}, overlayMaps, {data.elems[i].name : layer});
map.addLayer (layer);
}
map.addControl (new L.Control.Layers (baseMaps, overlayMaps));
}
});
My question is how to do line #9. data.elems[i].name doesn't want to work. I get this error missing : after property id, pointing right to the . after data. Any ideas?
missing : after property id, pointing right to the.afterdata. Sorry for not including that.