Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8325315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:23:28+00:00 2026-06-09T00:23:28+00:00

I have a dynamically-generated object that looks like this: colorArray = { AR: #8BBDE1,

  • 0

I have a dynamically-generated object that looks like this:

colorArray = { 
    AR: "#8BBDE1", 
    AU: "#135D9E", 
    AT: "#8BBDE1",
    ... }

I’m trying to use it to color a map by using this plugin and the ‘colors’ attribute during the call to the plugin. Like this:

$('#iniDensityMap').vectorMap({
    backgroundColor: '#c2e2f2',
    colors: colorArray,
    ... (some other params)
});

But it doesn’t color in the countries. When I hard code this in, it works fine – but it must be dynamically generated for this project, so something like this won’t work for me (although it does in fact color the map):

$('#iniDensityMap').vectorMap({
    backgroundColor: '#c2e2f2',
    colors: { AR: "#8BBDE1", AU: "#135D9E", AT: "#8BBDE1" },
    ... (some other params)
});

I’ve traced the issue far enough into the plugin to find it has something to do with this loop:

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]);
      }
    }
  }
},

I’ve also tried iterating through the colorArray object on my own, outside of the plugin and I’m running into the same issue. Whatever sits inside the for ( var x in obj ) isn’t firing. I’ve also noticed that colorArray.length returns undefined. Another important note is that I’ve instantiated var colorArray = {}; in a separate call, attempting to ensure that it is sitting at the global scope and able to be manipulated.

I’m thinking that the problem is either:

  1. The way I’m dynamically populating the object – colorArray[cCode] =
    cColor;
    (in a jQuery .each call)
  2. I’m once again confusing the differences between Arrays() and Objects() in javascript
  3. It is a scope issue perhaps?
  4. Some combination of everything above.

EDIT #1: I’ve moved my additional question about Objects in the Console in Firebug to a new post HERE. That question deals more specifically with Firebug than the underlying JS problem I’m asking about here.

Edit #2: Additional info
Here’s the code I’m using to dynamically populate the Object:

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();

This function is then called on a click event of a checkbox elsewhere on the page. The Objects iniDensityData and colorArray are declared in the head of the html file – hoping that keeps them in global scope:

<script type="text/javascript">
    //Initialize a bunch of variables in the global scope
    iniDensityData = {};
    colorArray = {};
</script>

And finally, here’s a snippet from the XML file that is being read:

<?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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T00:23:31+00:00Added an answer on June 9, 2026 at 12:23 am

    Solved it! Originally, I was calling the function parseDensityMapXML() and then immediately after it calling another function loadDensityMapXML() which took the object created dynamically in the first function and iterated through it. Problem was, it wasn’t called as a callback from the first function, so was firing before the Object had even been built.

    To fix, I just amended the first function mentioned above to call the second function after the .each() was finished creating the objects:

    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();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my web app I have a dynamically generated form that I use to
I have dynamically generated strings like @#@!efq@!#! , and I want to remove specific
I have a dynamically generated rss feed that is about 150M in size (don't
I have a series of dynamically generated inputs that I need to have ajax
I have a trivia page that is dynamically generated by some javascript code. It
I have content that is being dynamically generated. How to I listen to a
I have a complex object that if I render in this way: <%=Html.EditorFor(x =>
I have a section in my GUI that is generated dynamically according to a
I have this code (part of bigger script): flashvars.xmlSource = datasource.xml; datasource.xml looks like:
I have a form representing a survey that is dynamically generated based on some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.