Reputation: 2249
I want to iterate an array of objects to create one object, the properties of which are named for the current object in the iteration and the values of which use data from the current.
$.each(data, function(idx, obj) {
baseLayers[idx] = new Array();
baseLayers[idx][obj.name] = new L.TileLayer(obj.url, {maxZoom: 18, id: obj.key, attribution: mbAttr});
});
at the end, I want to get result an object like this
baseLayers = {
'Map Box': new L.TileLayer('https://{s}.tiles.mapbox.com/v3/{id}/{z}/{x}/{y}.png', {maxZoom: 18, id: 'examples.map-i875mjb7', attribution: mbAttr}),
'OSM': new L.TileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {maxZoom: 18, attribution: mbAttr})
};
Upvotes: 1
Views: 174
Reputation: 171679
Without seeing what data looks like I believe you want:
var baseLayers = {};
$.each(data, function(idx, obj) {
baseLayers[obj.name] = new L.TileLayer(obj.url, {maxZoom: 18, id: obj.key, attribution: mbAttr});
});
You are trying to add an extra level which is not shown in your expected output
Upvotes: 1
Reputation: 77482
Try this
var baseLayers = {}
$.each(data, function(idx, obj) {
baseLayers[obj.name] = new L.TileLayer(obj.url, {maxZoom: 18, id: obj.key, attribution: mbAttr});
});
Upvotes: 2