Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript-使用for…in迭代对象时出现问题_Javascript_Jvectormap - Fatal编程技术网

Javascript-使用for…in迭代对象时出现问题

Javascript-使用for…in迭代对象时出现问题,javascript,jvectormap,Javascript,Jvectormap,我有一个动态生成的对象,如下所示: colorArray = { AR: "#8BBDE1", AU: "#135D9E", AT: "#8BBDE1", ... } 我正试图在调用插件的过程中使用和“colors”属性来给地图上色。像这样: $('#iniDensityMap').vectorMap({ backgroundColor: '#c2e2f2', colors: colorArray, ... (some other

我有一个动态生成的对象,如下所示:

colorArray = { 
    AR: "#8BBDE1", 
    AU: "#135D9E", 
    AT: "#8BBDE1",
    ... }
我正试图在调用插件的过程中使用和“colors”属性来给地图上色。像这样:

$('#iniDensityMap').vectorMap({
    backgroundColor: '#c2e2f2',
    colors: colorArray,
    ... (some other params)
});
但它在其他国家不着色。当我硬编码这个时,它工作得很好-但是它必须为这个项目动态生成,所以类似的东西对我来说不起作用(尽管它实际上会给地图上色):

我已经将这个问题追溯到插件中,发现它与这个循环有关:

setColors: function(key, color) {
  if (typeof key == 'string') {
    this.countries[key].setFill(color);
  } else {

    var colors = key; //This is the parameter passed through to the plugin

    for (var code in colors) {

      //THIS WILL NOT GET CALLED

      if (this.countries[code]) {
        this.countries[code].setFill(colors[code]);
      }
    }
  }
},
我还试着在插件之外自己迭代
colorArray
对象,我遇到了同样的问题。位于for(obj中的var x)的内部的任何东西都不会触发。我还注意到,
colorArray.length
返回未定义的值。另一个重要的注意事项是,我实例化了
var colorArray={}在单独的调用中,尝试确保它位于全局范围并能够被操纵

我认为问题在于:

  • 动态填充对象的方式-
    colorArray[cCode]=
    彩色(在jQuery中。每次调用)
  • 我再次混淆了javascript中数组()和对象()之间的区别
  • 这也许是一个范围问题
  • 以上一切的某种组合
  • 编辑#1:我已将有关Firebug控制台中对象的附加问题移至新帖子。这个问题比我在这里要问的基本JS问题更具体地涉及Firebug

    编辑#2:其他信息 下面是我用来动态填充对象的代码:

    function parseDensityMapXML() {
    $.ajax({
        type: "GET",
        url: "media/XML/mapCountryData.xml",
        dataType: "xml",
        success: function (xml) {
            $(xml).find("Country").each(function () {
                var cName = $(this).find("Name").text();
                var cIniCount = $(this).find("InitiativeCount").text();
                var cUrl = $(this).find("SearchURL").text();
                var cCode = $(this).find("CountryCode").text();
    
                //Populate the JS Object
                iniDensityData[cCode] = { "initiatives": cIniCount, "url": cUrl, "name": cName };
                //set colors according to values of initiatives count
                colorArray[cCode] = getCountryColor(cIniCount);
            });
        }
    });
    } //end function parseDensityMapXML();
    
    然后在页面其他位置的复选框的单击事件中调用此函数。对象
    iniDensityData
    colorArray
    在html文件的头部声明-希望将它们保持在全局范围内:

    <script type="text/javascript">
        //Initialize a bunch of variables in the global scope
        iniDensityData = {};
        colorArray = {};
    </script>
    
    
    //在全局范围内初始化一组变量
    iniDensityData={};
    colorArray={};
    
    最后,这里是正在读取的XML文件的一个片段:

    <?xml version="1.0" encoding="utf-8"?>
    <icapCountryData>
      <Country>
        <Name>Albania</Name>
        <CountryCode>AL</CountryCode>
        <InitiativeCount>7</InitiativeCount>
        <SearchURL>~/advance_search.aspx?search=6</SearchURL>
      </Country>
      <Country>
        <Name>Argentina</Name>
        <CountryCode>AR</CountryCode>
        <InitiativeCount>15</InitiativeCount>
        <SearchURL>~/advance_search.aspx?search=11</SearchURL>
      </Country>
      ... and so on ...
    </icapCountryData>
    
    
    阿尔巴尼亚
    艾尔
    7.
    ~/advance\u search.aspx?search=6
    阿根廷
    应收账
    15
    ~/advance\u search.aspx?search=11
    ... 等等
    
    解决了它最初,我调用函数
    parseDensityMapXML()
    ,然后紧接着调用另一个函数
    loadDensityMapXML()
    ,该函数获取在第一个函数中动态创建的对象并对其进行迭代。问题是,它不是作为第一个函数中的回调调用的,所以在构建对象之前就触发了

    为了修复,我刚刚修改了上面提到的第一个函数,在之后调用第二个函数。each()完成了对象的创建:

    function parseDensityMapXML() {
    $.ajax({
        type: "GET",
        url: "media/XML/mapCountryData.xml",
        dataType: "xml",
        success: function (xml) {
            $(xml).find("Country").each(function () {
                var cName = $(this).find("Name").text();
                var cIniCount = $(this).find("InitiativeCount").text();
                var cUrl = $(this).find("SearchURL").text();
                var cCode = $(this).find("CountryCode").text();
    
                //Populate the JS Object
                iniDensityData[cCode] = { "initiatives": cIniCount, "url": cUrl, "name": cName };
                //set colors according to values of initiatives count
                colorArray[cCode] = getCountryColor(cIniCount);
            });
    
            /* and then call the jVectorMap plugin - this MUST be done as a callback
               after the above parsing.  If called separately, it will fire before the
               objects iniDensityData and colorArray are created! */
            loadDensityMapXML();
        }
    });
    } //end function parseDensityMapXML();
    

    至少你肯定把[]和{}搞混了。{}没有长度属性,除非您显式添加一个。另外:当您在for循环上设置断点时,颜色的值是您期望的值吗?这里只是在黑暗中拍摄一张照片……您提到您正在使用
    jQuery.each
    函数。您是否碰巧在该函数中使用了
    this
    关键字?e、 g.
    这个国家
    这个
    绑定不正确是JavaScript中最常见的错误原因之一。@DanDaviesBrackett是的,我很确定我想使用{}(Object,correct?)这样。length对此不起作用-但肯定for/in循环应该?@HerroRygar非常确定这是正确的,在jQuery ajax调用的成功回调中,我正在这样做:
    $(xml).find(“Country”).each(函数(){var cCode=$(this.find(“CountryCode”).text();…更多内容}
    ,然后像这样填充colorArray:
    colorArray[cCode]=cColor;
    function parseDensityMapXML() {
    $.ajax({
        type: "GET",
        url: "media/XML/mapCountryData.xml",
        dataType: "xml",
        success: function (xml) {
            $(xml).find("Country").each(function () {
                var cName = $(this).find("Name").text();
                var cIniCount = $(this).find("InitiativeCount").text();
                var cUrl = $(this).find("SearchURL").text();
                var cCode = $(this).find("CountryCode").text();
    
                //Populate the JS Object
                iniDensityData[cCode] = { "initiatives": cIniCount, "url": cUrl, "name": cName };
                //set colors according to values of initiatives count
                colorArray[cCode] = getCountryColor(cIniCount);
            });
    
            /* and then call the jVectorMap plugin - this MUST be done as a callback
               after the above parsing.  If called separately, it will fire before the
               objects iniDensityData and colorArray are created! */
            loadDensityMapXML();
        }
    });
    } //end function parseDensityMapXML();