0

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

2
  • in ES2015 you can write {[color]: {}} Other way you should define all of them key-by-key: o[color] = {}; Commented Jun 10, 2016 at 10:43
  • are you sure this is the correct json??? Commented Jun 10, 2016 at 10:47

2 Answers 2

1

Something like this should do the job:

var params = {}; for (var prop2 in drivers[prop]) { var color = drivers[prop][prop2]['truck_color']; params[color] = { current: 0, polygonOptions: { fillColor: color, fillOpacity: 0.7, strokeColor: '#2143AA', strokeWeight: 2, suppressUndo: true } }; } maps.setColors(params); 
Sign up to request clarification or add additional context in comments.

Comments

0

I think that you should put it like this:

var prove = { [color]: { current: 0, polygonOptions: { fillColor: color, fillOpacity: 0.7, strokeColor: '#2143AA', strokeWeight: 2, suppressUndo: true } }}; 

With brakets. See console output in this codepen: http://codepen.io/anon/pen/vKGNbB?editors=1111

2 Comments

What's the output you get using [color]?
Object { #00FF00: Object { current: 0, polygonOptions: Object {} } }

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.