Removing array items by index

When removing items from an array via its index number, remember to always start from the end and work your way to the beginning. If you don’t, and are removing more than one item, the index you where looking for will no longer be the correct item because one or more have already been removed and the indexes have been reassigned to fill the gap.

Correct Example: (I am using the Cairngorm framework in this example.)

for (var j:int=grid1.selectedIndices.length-1; j>=0; j--) {
model.grid1data.removeItemAt(grid1.selectedIndices[j]);
}

Also remember, that “indices” is the plural form of “index”.

 

 

Advertisements