Adding An Array Element At Arbitrary Index

From ActionScriptWiki

Jump to: navigation, search

This snippet will add an object at an arbitrary index while preserving the order of the array. The method can be optimized a little bit more if array.length is known of course.

function addObjectAtIndex( array: Array, object: Object, index: int ): void
{
	if( 0 == index )
		array.unshift( object );
	else
	if( array.length == index )
		array.push( object );
	else
		array.splice( index, 0, object );
}
Personal tools