How To The Write `arrayUnshift( )` Function In ColdFusion?
Description:-
This function adds one or more elements to the original array’s beginning and returns the modified array’s length.
arrayUnshift( array, object );
Member Function Syntax:-
array.unshift( object );
Attributes:-
array:-
This is a required argument, the name of the array object where an object is to be added. The original array.
object:-
This is a required argument, This is a new object that needs to be added.
Example:-
<cfscript>
marvelArray = [ "Black Widow", "Scarlet Witch", "Thor", "Black Panther", "Iron Man", "Spider-Man", "Vision", "Captain Marvel" ];
writeOutput( "Old Array:-" );
writeDump( marvelArray );
newArrayLength = arrayUnshift( marvelArray, "Captain America" );
writeOutput( "Modified Array Length:- #newArrayLength#<br>" );
writeOutput( "Modified Array:-" );
writeDump( marvelArray );
</cfscript>
Result:-
Old Array:-
array | |
1 | Black Widow |
2 | Scarlet Witch |
3 | Thor |
4 | Black Panther |
5 | Iron Man |
6 | Spider-Man |
7 | Vision |
8 | Captain Marvel |
Modified Array Length:- 9
Modified 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 |
An example by using a member function:-
<cfscript>
marvelArray = [ "Black Widow", "Scarlet Witch", "Thor", "Black Panther", "Iron Man", "Spider-Man", "Vision", "Captain Marvel" ];
writeOutput( "Old Array:-" );
writeDump( marvelArray );
newArrayLength = marvelArray.unshift( "Captain America" );
writeOutput( "Modified Array Length:- #newArrayLength#<br>" );
writeOutput( "Modified Array:-" );
writeDump( marvelArray );
</cfscript>
Result:-
Old Array:-
array | |
1 | Black Widow |
2 | Scarlet Witch |
3 | Thor |
4 | Black Panther |
5 | Iron Man |
6 | Spider-Man |
7 | Vision |
8 | Captain Marvel |
Modified Array Length:- 9
Modified 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 |