Search code examples
javascriptvariablesd3.jscountrydatamaps

D3.JS datamaps country name variable


I want to enter the country name as a variable but I can't.

var interval_1=window.setInterval(function() {
  var name = "USA";
  map.updateChoropleth({
  name : colors(Math.random() * 10),
  });
}, 2000);

Solution

  • What you want is to use a variable as a key in a JavaScript object literal. This has already been answered here: Using a variable for a key in a JavaScript object literal

    This would give the result you expect:

    var interval_1=window.setInterval(function() {
        var name = "USA";
        var country = {}
        country[name] = colors(Math.random() * 10)
        map.updateChoropleth(country);
    }, 2000);