javascript - 循环遍历数组并设置系列对象 jVectormap 的值

标签 javascript jquery arrays object jvectormap

我正在与 jVectormap 合作插件。

我有一个国家/地区代码数组,currentCodes,是我在开始时声明的。在页面的下方,我正在使用该插件的内置“系列”功能,该功能使我能够为某些国家/地区提供不同的默认颜色。在 series: { } 下的 values: { } 中,我再次写出了 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
      }]
    }        

  ……

});

关于javascript - 循环遍历数组并设置系列对象 jVectormap 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18400386/

相关文章:

javascript - 仅在弹出窗口启动后打印 Javascript

javascript - 如何检查 mobx observable 是否有订阅者?

javascript - 难道真的是chrome和ie的数组排序bug?

python - 从满足 bool 条件的 NumPy 数组中获取(列,行)索引

javascript - 如果屏幕宽度大于,如何停止调整大小和加载时的点击事件?

javascript - 如何使用 sendy 检查用户状态(订阅与否)

javascript - 在月/日期选择器上禁用月份

php - AJAX 调用未发送成功响应

javascript - 使用 JQuery 控制 <select> 的打开/关闭

javascript - 如何在一个指令上使用多个 ngFor 进行属性绑定(bind)