循环遍历数组和设置系列对象的值,jVectormap

时间:2013-08-23 10:20:26

标签: javascript jquery arrays object jvectormap

我正在使用jVectormap插件。

我有一系列国家/地区代码currentCodes,我在一开始就宣布这一点。在页面的下方,我正在使用插件的内置“系列”功能,这使我能够为某些国家/地区提供不同颜色的默认颜色。在values: { } series: { }currentCodes下,我再次写出了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 } }] } …… }); 中的每个值,并在系列中将它们设置为1。这很好。

currentCodes

但我想要的是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, } } }] } …… }); 数组中的任何值自动设置为1的方法。我知道使用for循环完全是错误的语法,但它可能会证明我需要的东西:

{{1}}

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

1 个答案:

答案 0 :(得分: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
      }]
    }        

  ……

});