ArrayNew

Return new array

Method Signature

ArrayNew()

Arguments

This function does not accept any arguments

Examples

Create the One dimensional array

Uses the arrayNew function to create the new array

Run Example

newArray = arrayNew( 1 );
someArray =
// Transpiler workaround for BIF return type
(( arg1, arg2, arg3, arg4 ) => {
	arraySet( arg1, arg2, arg3, arg4 );
	return true;
})( newArray, 1, 4, "All is well" );
writeOutput( JSONSerialize( newArray ) );

Result: ["All is well", "All is well", "All is well", "All is well"]

Create the Two dimensional array

Uses the arrayNew function to create the new array

Run Example

newArray = arrayNew( 2 );
newArray[ 1 ][ 1 ] = "First value";
newArray[ 1 ][ 1 ] = "First value";
newArray[ 1 ][ 2 ] = "First value";
newArray[ 2 ][ 1 ] = "Second value";
newArray[ 2 ][ 2 ] = "Second value";
writeOutput( JSONSerialize( newArray ) );

Result: [["First value", "First value"],["Second value", "Second value"]]

Create unsynchronized array

Uses the arrayNew function to create the new unsynchronized array

Run Example

newArray = arrayNew( 1, false );
newArray.append( "one" );
writeOutput( JSONSerialize( newArray ) );

Result: ["one"]

Create an array using implicit notation

Instead of using arrayNew you can also create an array using square brackets.

Run Example

newArray = [ 
	"one",
	"two"
];
writeOutput( JSONSerialize( newArray ) );

Result: ["one", "two"]

Create an array with data type

When using data types on array creation, items are converted if possible, otherwise an error is thrown.

typedArray = arrayNew[ "boolean" ]( 1 );
typedArray[ 1 ] = true;
typedArray[ 2 ] = "true";
typedArray[ 3 ] = 1;
typedArray[ 4 ] = "1";
typedArray[ 5 ] = "yes";
typelessArray = arrayNew( 1 );
typelessArray[ 1 ] = true;
typelessArray[ 2 ] = "true";
typelessArray[ 3 ] = 1;
typelessArray[ 4 ] = "1";
typelessArray[ 5 ] = "yes";
writeOutput( JSONSerialize( [
	typedArray,
	typelessArray
] ) );

Result: [[true,true,true,true,null,true],[true,"true",1,"1",null,"yes"]]

Additional Examples

Run Example

a = arrayNew( 1 );
// Implicit array notation
a.append( [] );
// with values
a.append( [
	"a",
	"b",
	3,
	4,
	[],
	{
		COMPLEX : true
	},
	queryNew( "id,date" )
] );
dump( a );

Last updated

Was this helpful?