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 8023919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:42:25+00:00 2026-06-04T22:42:25+00:00

I have an array filled with strings that have the same name as Google

  • 0

I have an array filled with strings that have the same name as Google Maps Polygon Objects I’ve created. I’d like to iterate through the array to set a specific option. Here’s the code:

for (var i = 0; i < statesPoly.length; i++) {
    google.maps.event.addListener(statesPoly[i], 'mouseover', function() {
        statesPoly[i].setOptions({ strokeWeight: '2' });
    });
}

When executed I get “Cannot call method ‘setOptions’ of undefined” as the script seems to be using statesPoly[i] literally. When I replace statesPoly[i] with, for example, statesPoly[11], the script works as expected.

The loop also works as expected when I try something like this:

for (var i = 0; i < statesPoly.length; i++) {
    alert(statesPoly[i].strokeColor);
}

What am I doing wrong?

UPDATE:

Getting closer here. I believe the reason that using this works in some cases is because my function is expecting an object and I am giving it a string. Could this be the case?

alert(statesPoly[0]);
        google.maps.event.addListener(sarawakPoly, 'mouseover', function() {
            $("#"+statesPoly[0]).addClass("highlight");
            sarawakPoly.setOptions({ strokeWeight: '2' });
            //infowindow.open(map,marker);
        });

The code above will alert with SarawakPoly, and using statesPoly[0] as a string in the ID works as expected. This

 alert(statesPoly[0]);
        google.maps.event.addListener(statesPoly[0], 'mouseover', function() {
            $("#"+statesPoly[0]).addClass("highlight");
            statesPoly[0].setOptions({ strokeWeight: '2' });
            //infowindow.open(map,marker);
        });

Does not work because “Uncaught TypeError: Cannot read property ‘mouseover’ of undefined”

If I’m right, how to I get my JS to cast my array variable as an object?

  • 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-04T22:42:27+00:00Added an answer on June 4, 2026 at 10:42 pm

    This is a very common mistake in JavaScript code:

    for (var i = 0; i < n; i++) {
        registerSomeCallback(function () {
            console.log(i);
        });
    }
    

    On each loop iteration the variable i is incremented, and becasue of JavaScript’s lexical scoping the functions defined on each iteration share the same i variable. This means that when the callback is invoked (in your case when a Google Maps event occurs), i will always be the last value the loop reached.

    It’s as if you’re doing this:

    var i, fn;
    
    i = 0;
    fn = function () { alert(i); };
    fn(); // will alert "0"
    i = 1;
    fn(); // i has changed, will now alert "1"
    i = 2;
    fn(); // i has changed again, will now alert "2"
    

    You need to make sure there’s a new variable scope for each iteration of your loop, for example:

    for (var i = 0; i < n; i++) {
        (function (n) {
            registerSomeCallback(function () {
                console.log(n);
            });
        }(i));
    }
    

    In this version of the code, each callback is defined in its own variable scope with its own counter variable (which you could still call i, but I’ve called n to make the example more clear).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string array that is being filled dynamically.... I would like to
I have an array filled with paths looking like this: library/main/single/list.php library/article/grid/thumbs.php library/footer/tiny.php These
I have an array that collects strings of questions passed as such: $myQuestionsPass =
We have an array filled with category names (many of them repeats) and we
I have dynamic array filled with bytes, which are read from .raw file with
I have a byte array filled from a file uploaded. But, in another part
I have a byte array filled with hex numbers and printing it the easy
I have a 2d dbl array filled with data, which i can call anywhere
I have 3 arrays of data, that are filled by reading off of an
I have array of objects: var aoo = [{},{},{},....{},{},{}]; I need a optimized function

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.