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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:21:10+00:00 2026-05-27T20:21:10+00:00

When working with Google Maps V3 markers, I make sure to store the name

  • 0

When working with Google Maps V3 markers, I make sure to store the name of each marker in an array so I can quickly remove all of them from the map at once. For some reason though, when I call my function that is supposed to iterate through the entire array, removing all markers a long the way, the function returns undefined after only removing a few of the markers.

The array (markersArray) before the function:

["markerZip02111", "markerZip02139", "markerZip01002", "markerZip94602", "markerZip02460"]

The function code:

function removeAllMarkers(exceptId) {
    $.each(markersArray, function(index, value) {
        if(value != exceptId) {
            eval(value+".setMap(null);");
            markersArray.splice(value, 1);
            console.log(value);
        }
    });
}

What the console displays:

markerZip02111
markerZip01002
markerZip02460
undefined

The array after the function is run:

["markerZip94602", "markerZip02460"]

Clearly the array is running successfully until it hits the “undefined” value and then it stops. What can I do to get around this problem??

  • 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-05-27T20:21:11+00:00Added an answer on May 27, 2026 at 8:21 pm

    I’m pretty sure the reason you’re getting an undefined value during iteration when your starting array doesn’t have any undefined values in it is that you’re removing items from the array while iterating through it. I guess this confuses the jQuery $.each() iterator.

    If you look at your output, what is happening is this:

    1st Iteration
        index === 0, array is["markerZip02111", "markerZip02139", "markerZip01002",
                              "markerZip94602", "markerZip02460"]
        item 0 "markerZip02111" gets removed, shifting all the later elements up
    2nd Iteration
        index === 1, but now array is ["markerZip02139", "markerZip01002",
                                       "markerZip94602", "markerZip02460"]
        item 1 "markerZip01002" gets removed, shifting all the later elements up
    3rd Iteration
        index ===2, but now array is ["markerZip01002", "markerZip94602",
                                      "markerZip02460"]
        so the last item "markerZip02460" gets removed
    4th Iteration
        index === 3, but now array only has two elements so value
        at that index is undefined.
    

    Notice that two of the items never got evaluated: the iterator skipped over them because you changed their indexes by removing items.

    If you must remove items as you go it is easy with a conventional for loop that iterates backwards such that removing items won’t screw up the loop counter. (Or you can use a conventional for loop to go forwards as long as you adjust the counter variable each time you remove an item.)

    Also, when you do splice you need to pass the index of the item as the first parameter, not the value of the item. So markersArray.splice(index, 1); not markersArray.splice(value, 1);.

    So, something like:

    function removeAllMarkers(exceptId) {
       var value;
       for (var i = markersArray.length - 1; i >= 0; i--) {
          value = markersArray[i];
          if (value != exceptId) {
             markersArray.splice(i, 1);
             eval(value+".setMap(null);");
             console.log(value + " removed");
          }
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working with the google maps API, and testing a section which geocodes all
I have a v3 Google Maps working fine with dynamic markers but I am
I have been working with google maps and now have a requirement to make
Am working on Google maps and Bing Maps and want to add multiple markers
I just started working using Google Maps API yesterday, and trying to set up
I'm currently working on a Google Maps project and am implementing a search function.
I've been working on a Google Maps based Rails app and it was working
I am working with both amq.js (ActiveMQ) and Google Maps . I load my
I am working on an Android app that utilizes the Google Maps API MapView,
I am starting with google maps. just learning. while ago, it was working. now

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.