Chapter 3 DynaScript Predefined Objects


Array methods

The Array object has these methods:

join method

Syntax

Array.join( separator )

Description

Converts the elements of the array into strings which are then concatenated. If no separator is given, commas will separate the strings.

Return

String object.

Example

This example lists the elements of the Array separated by the
"&" character:

<!--SCRIPT 
myArray = new Array();
myArray[0] = "carrots";
myArray[1] = "corn";
myArray[2] = "green beans";
myArray[3] = "celery";
document.WriteLn( myArray.join( " & " ) );
-->

The output from this example is:

carrots & corn & green beans & celery

reverse method

Syntax

Array.reverse( )

Description

Reverses the order of the elements in the array.

Return

Array object.

Example

This example reverses and displays the content of the myArray array:

<!--SCRIPT 
myArray = new Array();
myArray[0] = "carrots";
myArray[1] = "corn";
myArray[2] = "green beans";
myArray[3] = "celery";
document.WriteLn( myArray.reverse());
-->

sort method

Syntax

Array.sort( [compareFunction] )

Description

Sorts the elements of an array. A compareFunction can optionally be used to sort the array or the default may be used. The compare function takes two arguments a, b. If

Return

Array object.

Example

This example sorts the array alphabetically and displays the results:

<!--SCRIPT 
myArray = new Array();
myArray[0] = "carrots";
myArray[1] = "corn";
myArray[2] = "green beans";
myArray[3] = "celery";
document.WriteLn( myArray.sort() );
-->

toString method

Syntax

Array.toString( )

Description

Converts the elements of the array to concatenated strings, separated by commas.

Return

String.

Example

This example converts the array elements to strings and displays them:

<!--SCRIPT 
myArray = new Array();
myArray[0] = "carrots";
myArray[1] = "corn";
myArray[2] = "green beans";
myArray[3] = "celery";
document.WriteLn( myArray.toString() );
-->

 


Copyright © 1999 Sybase, Inc. All rights reserved.