Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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 <code>$.each()</code> iterator.</p> <p>If you look at your output, what is happening is this:</p> <pre><code>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. </code></pre> <p>Notice that two of the items never got evaluated: the iterator skipped over them because you changed their indexes by removing items.</p> <p>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.)</p> <p>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 <code>markersArray.splice(index, 1);</code> <em>not</em> <code>markersArray.splice(value, 1);</code>.</p> <p>So, something like:</p> <pre><code>function removeAllMarkers(exceptId) { var value; for (var i = markersArray.length - 1; i &gt;= 0; i--) { value = markersArray[i]; if (value != exceptId) { markersArray.splice(i, 1); eval(value+".setMap(null);"); console.log(value + " removed"); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload