繁体   English   中英

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

[英]Looping through array and setting values for series object, 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
          }
      }]
    }        

  ……

});

但是我想要的是将currentCodes数组中的任何值自动设置为1的方法。我知道使用for循环完全是错误的语法,但它可能会证明我需要的东西:

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

          }
      }]
    }        

  ……

});

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

尝试这个。

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

  ……

});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM