2

在與谷歌地圖V3標誌的工作,我一定要每個標記的名稱存儲在數組中,所以我可以很快地從地圖上立即將它們全部刪除。不過由於某種原因,當我調用我的函數時,應該遍歷整個數組,在刪除所有標記的過程中,函數僅在刪除一些標記後返回undefined。

函數之前的陣列(markersArray):

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

功能碼:

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

什麼控制檯顯示:

markerZip02111 
markerZip01002 
markerZip02460 
undefined 

功能之後的陣列運行:

["markerZip94602", "markerZip02460"] 

顯然,直至碰到「未定義」值,然後將其停止陣列成功運行。我能做些什麼來解決這個問題?

+3

你可以很容易地避免'的eval()'如果你在一個對象設置每個'markerZipxxxxx'項目,而不是讓他們的變量。如果它們是全局變量,那麼您可以執行'window [value] && window [value] .setMap(null);' – 2011-12-31 03:28:45

+1

在遍歷數組時,「拼接」數組似乎不明智。你究竟想要完成什麼? – Domenic 2011-12-31 03:29:46

+0

我不是我:他們是全局變量,我不確定你的意思是窗口[價值]。 – Colin 2011-12-31 04:11:33

回答

3

我敢肯定迭代時,你的出發陣列沒有任何不確定的值時你得到一個未定義值的原因,那就是你從數組刪除項目,同時通過它迭代。我想這會混淆jQuery $.each()迭代器。

如果你看看你的輸出,正在發生的事情是這樣的:

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. 

注意兩個項目從來沒有得到評估:迭代器跳過它們,因爲你通過刪除項目改變了他們的索引。

如果你去很容易與傳統的for循環遍歷倒退,使得移除項目不會搞砸循環計數器必須刪除的項目。 (或者,只要每次移除物品時調整計數器變量,就可以使用常規的for循環前進。)

另外,當您進行拼接時,您需要傳遞物品的索引作爲第一個參數,而不是項目的值。所以markersArray.splice(index, 1);不是markersArray.splice(value, 1);

所以,像這樣:

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

謝謝!這是一個寫得很好的解釋。我完全明白現在出了什麼問題!謝謝!我在使用Google地圖時遇到問題,現在不能刪除標記,但它與循環停止無關。 – Colin 2011-12-31 04:34:25

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

當我添加!= undefined值時,循環在達到未定義值時仍然停止。 – Colin 2011-12-31 03:42:26

+0

我剛更新。 – dotnetstep 2011-12-31 05:10:09

2

我覺得dotnetstep釘,但你也可以嘗試包裹$.each用一個try/catch內部的邏輯更廣泛的處理:

http://www.w3schools.com/js/js_try_catch.asp

祝你好運!

+0

問題是沒有錯誤正在傳遞。問題在於當「undefined」通過時,循環停止。 – Colin 2011-12-31 04:09:36

+0

@ Colin-一個try/catch循環防止程序在遇到錯誤時停止(除非你明確地告訴它停止在「catch」部分)。 ;) – 2011-12-31 18:39:41