Javascript Object Extension
<p>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:</p>

<pre><code>$.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));
    }
});
</code></pre>

<p>My question is how to do line #9. <code>data.elems[i].name</code> doesn't want to work. I get this error <code>missing : after property id</code>, pointing right to the <code>.</code> after <code>data</code>. Any ideas?</p>
