How To The Write `arraySwap( )` Function In ColdFusion?
Description:-
This function swaps the array values of an array at specified positions. This function is more efficient than multiple <cfset> tags.
arraySwap( array, position1, position2 );
The function operates in place and returns a boolean ( True or False ) to indicate the success or failure of the operation.
Member Function Syntax:-
array.swap( position1, position2 );
Attributes:-
array:- This is a required argument, the name of the array object for which positions will be swapped.
position1:- This is a required argument and it is a numeric field, the position of the first element to swap.
position2:- This is a required argument and it is a numeric field, the position of the second element to swap.
Example:-
<cfscript>
marvelArray = [ "Captain America", "Black Widow", "Scarlet Witch", "Thor", "Black Panther", "Iron Man", "Spider-Man", "Vision", "Captain Marvel" ];
writeOutput( "Old Array:-" );
writeDump( marvelArray );
arraySwap( marvelArray, 2, 8 );
writeOutput( "Modified Array:-" );
writeDump( marvelArray );
</cfscript>
Result:-
Old Array:-
| array | |
| 1 | Captain America |
| 2 | Black Widow |
| 3 | Scarlet Witch |
| 4 | Thor |
| 5 | Black Panther |
| 6 | Iron Man |
| 7 | Spider-Man |
| 8 | Vision |
| 9 | Captain Marvel |
Modified Array:-
| array | |
| 1 | Captain America |
| 2 | Vision |
| 3 | Scarlet Witch |
| 4 | Thor |
| 5 | Black Panther |
| 6 | Iron Man |
| 7 | Spider-Man |
| 8 | Black Widow |
| 9 | Captain Marvel |
Here we swap marvelArray array values “Black Widow” with “Vision”, whose positions are 2 and 8.
