Chapter 3 DynaScript Predefined Objects
Enables the creation and manipulation of arrays.
Standard: ECMAScript
To use an Array property:
Array.propertyName
To use an Array method:
Array.MethodName( parameter )
The Array object has both a built-in object as well as constructors that allow users to create instances of an Array object. When referring to Array in the syntax of this chapter, we are referring to an instance of an array, not the built-in Array object.
Calling an array as a function or as a constructor has equivalent results--both create and initialize a new Array object.
To create an Array with a constructor:
myArray = new Array( [item0 [, item1 [, item2...]]] );
myArray = new Array( length )
It is generally more efficient to create an array with the number of elements you expect to use (or approximately), rather than growing the Array dynamically.
<!--SCRIPT myArray = new Array(4) myArray[0]= "number one"; myArray[1]= "number two"; myArray[2]= "number three"; myArray[3]= "number four"; document.WriteLn(myArray); -->
Array.length
Returns a value one higher than the highest element in the array.
Integer
<!--SCRIPT myArray = new Array(); myArray[0] = "carrots"; myArray[1] = "corn"; myArray[6] = "green beans"; document.WriteLn( myArray.length ); -->
Array.join( separator )
Converts the elements of the array into strings, which are then concatenated. If no separator is given, commas separate the strings.
String object
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
Array.reverse( )
Reverses the order of the elements in the array.
Array object.
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()); -->
The output from this example is:
celery & green beans & corn & carrots
Array.sort( [compareFunction] )
Sorts the elements of an array. You can use a compareFunction to sort the array or you can use the default. The compare function takes two arguments, a and b. If:
Array object
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() ); -->
Array.toString( )
Converts the elements of the array to concatenated strings, separated by commas.
String
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 © 2001 Sybase, Inc. All rights reserved. |
![]() |