Question

I am working with the jVectormap plugin.

I have an array of country codes, currentCodes, that I am declaring at the start. Further down the page, I am working with the plugin's built-in "series" feature which enables me to give certain countries different colours to the default. In values: { } under series: { } further down I have written out each of the values in currentCodes again and set them to 1 in the series. This is working fine.

jQuery.noConflict();
jQuery(function(){
  var $ = jQuery;
  var currentCodes = ["GG","IE","IM","JE","_22","_25","_23","_24"];
  $('#map').vectorMap({
    map: 'world_mill_en',
    backgroundColor: '#b0e0fb',
    ……
    series: {
      regions: [{
        scale: ['#008d48'],
        normalizeFunction: 'polynomial',
        values: {
          "GG": 1,
          "IE": 1,
          "IM": 1,
          "JE": 1,
          "_22": 1,
          "_25": 1,
          "_23": 1,
          "_24": 1
          }
      }]
    }        

  ……

});

But what I would like is a way for any values in the currentCodes array to automatically be set to 1. I know using a for loop is completely the wrong syntax here but perhaps it will demonstrate what I need:

jQuery.noConflict();
jQuery(function(){
  var $ = jQuery;
  var currentCodes = ["GG","IE","IM","JE","_22","_25","_23","_24"];
  $('#map').vectorMap({
    map: 'world_mill_en',
    backgroundColor: '#b0e0fb',
    ……
    series: {
      regions: [{
        scale: ['#008d48'],
        normalizeFunction: 'polynomial',
        values: {

          // set each value in currentCodes array so it is 1
          var i;
          for (i = 0; i < currentCodes.length; i++) {
          currentCodes[i]: 1,
          }

          }
      }]
    }        

  ……

});

Thanks, any help would be greatly appreciated. I'm not very experienced with objects and properties syntax, which I believe is what is being used here...

Was it helpful?

Solution

Try this.

jQuery.noConflict();
jQuery(function(){
  var $ = jQuery;
  var currentCodes = ["GG","IE","IM","JE","_22","_25","_23","_24"];

  var values = {};
  jQuery.each(currentCodes, function(idx, value){
      values[value] = 1;
  })

  $('#map').vectorMap({
    map: 'world_mill_en',
    backgroundColor: '#b0e0fb',
    ……
    series: {
      regions: [{
        scale: ['#008d48'],
        normalizeFunction: 'polynomial',
        values: values
      }]
    }        

  ……

});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top