I have this code below and I'm trying to make the property name "color" in the "maps.setColors({" replaced with its value from the drivers array:
var drivers = {
"drivers":[
{
"driver_id":"101",
"driver_name":"B",
"truck_capacity":1000,
"truck_color":"#00FF00",
"truck_radio":"checked"
},
{
"driver_id":"102",
"driver_name":"A",
"truck_capacity":2000,
"truck_color":"#FF0000",
"truck_radio":""
}
]
};
for (var prop in drivers) {
if (drivers.hasOwnProperty(prop)) {
for (var prop2 in drivers[prop]) {
var color = drivers[prop][prop2]['truck_color'];
maps.setColors({
color: {
current: 0,
polygonOptions: {
fillColor: color,
fillOpacity: 0.7,
strokeColor: '#2143AA',
strokeWeight: 2,
suppressUndo: true
}
}
});
google.maps.event.addDomListener(document.getElementById(id), 'click', function() {
maps.setColor(color);
});
}
}
}
It is not working so basiclly I want the loop to produce dynamically a code like this:
maps.setColors({
'#00FF00': {
current: 0,
polygonOptions: {
fillColor: '#00FF00',
fillOpacity: 0.7,
strokeColor: '#2143AA',
strokeWeight: 2,
suppressUndo: true
}
},
'#FF0000': {
current: 0,
polygonOptions: {
fillColor: '#FF0000',
fillOpacity: 0.7,
strokeColor: '#2143AA',
strokeWeight: 2,
suppressUndo: true
}
}
});
How can I do that please?
Thanks
ES2015you can write{[color]: {}}Other way you should define all of them key-by-key:o[color] = {};