」工欲善其事,必先利其器。「—孔子《論語.錄靈公》
首頁 > 程式設計 > 循環時如何安全地刪除JavaScript中的數組項目?

循環時如何安全地刪除JavaScript中的數組項目?

發佈於2025-03-04
瀏覽:961

How to Safely Remove Array Items in JavaScript While Looping?
在JAVAScript中通過數組和刪除項目循環並刪除項目

解決此問題,請考慮以下替代方案:

[1。在splice()(i = 0,len = auction.auctions.auctions.length; i 2。 Iterate in Reverse

By iterating through the array in reverse order, the re-indexing after splice() will not affect the remaining items in the iteration.

var i = Auction.auctions.length 而(i--){ ... 如果 (...) { auction.auctions.splice(i,1); } }

通過採用以下方法之一,您可以通過數組有效迭代並刪除項目而不會遇到未定義的值問題並確保循環過程的完整性。

最新教學 更多>

免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。

Copyright© 2022 湘ICP备2022001581号-3