2010-01-13 37 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

相关问题