2013-08-23 47 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