2013-08-23 53 views
0

我正在使用jVectormap插件。循环访问系列对象的数组和设置值,jVectormap

我有一组国家代码,currentCodes,我在开始时声明。再往下看,我正在使用插件内置的“系列”功能,这使我可以给某些国家不同的颜色默认。在values: { }下的series: { }再往下,我已经写出了currentCodes中的每个值,并将它们设置为1。这工作正常。

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 
      } 
     }] 
    }   

    …… 

}); 

但我想是因为我知道使用for循环是完全错误的语法在这里,但也许它会表现出什么,我需要的currentCodes阵列中的任何值自动设置为1的方式:

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, 
      } 

      } 
     }] 
    }   

    …… 

}); 

谢谢,任何帮助将不胜感激。我对对象和属性语法不太熟悉,我相信这是在这里使用的...

回答

2

试试这个。

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 
     }] 
    }   

    …… 

}); 
+0

谢谢你,这工作得很好!会upvote,但我不能,因为我没有足够的声望点呢! – Sarah