2010-01-13 18 views
-1

如果我有一個數組AS3:在不同的地方拼接兩件物品?

private var temp:Array = [item1, item2, item3, item4, item5, item6, item7...etc]; 

兩個變量數組中的項:

private var firstPosition; 

private var secondPosition; 

有沒有辦法立刻刪除這兩個項目?

就是說,如果firstPosition = ITEM4,和secondPosition = item7 ...然後firstPosition =溫度[3]和secondPosition =溫度[6]

但是如果我寫:

temp.splice(firstPosition, 1); 

然後secondPosition是他們temp [5]而不是temp [6] ...因爲一個已經從數組中移除。

我一直在寫:

temp.splice(firstPosition,1); 
temp.splice(secondPosition-1,1); 

我不認爲這是正確的......特別是如果secondPosition是在「溫度」數組的開頭(即溫度[0])。

有沒有辦法一次從數組中刪除兩個項目,如果他們不是並排?

回答

0

開始從位置最大索引中刪除:

// it will not change firstPosition if firstPosition < secondPosition 
temp.splice(secondPosition, 1); 
temp.splice(firstPosition, 1); 

這不會降低指數影響的位置。

+0

如何對兩個變量firstPosition和secondPosition進行排序?使用indexOf? – redconservatory 2010-01-13 17:00:16

+0

您無法對變量進行排序。只需比較它們以確保'firstPosition secondPosition)swap_values_of(firstPosition,secondPosition)' – 2010-01-13 18:54:22