ArraySwap

Swaps array values of an array at specified positions.

This function is more efficient than multiple assignment statements

Method Signature

ArraySwap(array=[array], position1=[any], position2=[any])

Arguments

Argument
Type
Required
Description
Default

array

array

true

position1

any

true

The first position to swap

position2

any

true

The second position to swap

Examples

Swap the position of two values in an array

Run Example

superiorArray = [ 
	"Spider-Man",
	"Green Goblin",
	"Doctor Octopus",
	"Venom"
];
arraySwap( superiorArray, 1, 3 );
writeDump( superiorArray );

Result: ['Doctor Octopus', 'Green Goblin', 'Spider-Man', 'Venom']

Swap the position of two values in an array using the member function

Run Example

superiorArray = [ 
	"Spider-Man",
	"Green Goblin",
	"Doctor Octopus",
	"Venom"
];
superiorArray.swap( 1, 3 );
writeDump( superiorArray );

Result: ['Doctor Octopus', 'Green Goblin', 'Spider-Man', 'Venom']

Additional Examples

Run Example

superiorArray = [ 
	"Spider-Man",
	"Green Goblin",
	"Doctor Octopus",
	"Venom"
];
arraySwap( superiorArray, 1, 3 );
dump( superiorArray );
// member function
superiorArray.swap( 1, 3 );
dump( superiorArray );

Last updated

Was this helpful?