Array Object
An array is a programming construct fundamental to
nearly all languages today. Javascript is no different , providing the
capability to construct and work with arrays. An array is simply a container
holding a set of data elements. Each of the elements in an array is a separate
value, but they exist as part of the array and cannot be accessed except by
going through the array.
To define or access a particular element, you need
to add brackets and an index value to the array variable. For example, you can
define an array celled coffee as follows:
Coffee[0]= “Ethiopian sidamo”
Coffee[1]= ”kenyan”
Coffee[2]= “café Verona”
Coffee[3]= “Sumatra”
Coffee[4]= “costa rice”
Coffee[5]= “Columbian”
Coffee[6]= “bristan”
If you wanted to use any of these elements within a
script, you could access them using the array variable along with an index
value representing the location of the element within the array. Therefore , if
you wanted to write the following line:
My favorite coffee is Ethiopian Sidamo
You would code the following:
Document.write(“My favorite coffee is”+coffee[0])
If array are new to you , you might find it helpful
to think of an array as Javascript’s equivalent of a numbered list. It is
really no more complicated than that. For example, suppose you have a list of
ten items:
]
Javascript
]
Java
]
Delphi
]
C++
]
Visual Basic
]
Oracle Power Objects
]
SmallTalk
]
PowerBuilder
]
Paradox
]
Access
If you wanted to assemble
this group of items in a javascript array, it would look like the following:
devTools[0]=”Javascript”
devTools[1]=”Java”
devTools[2]=”Delphi”
devTools[3]=”C++”
devTools[4]=”Visual
Basic”
devTools[5]=”Oracle
power objects”
devTools[6]=”SmallTalk”
devTools[7]=”PowerBuilder”
devTools[8]=”Paradox”
devTools[9]=”Access”
Creating an Array
Object
Unlike a string, an array is
not recognized as a data type in Javascript , but you can work with them as
objects. The way in which you create Array objects varies depending on the
browser you are doing.
Array Objects
with Netscape Navigator 3.0
Netscape Navigator 3.0
enables you to work with arrays as true Javascript objects. You can create
instances of an Array object using the new operator. For example, if you wanted
to create the coffee array defined earlier, you would define it as follows:
var coffee=new
Array()
Coffee[0]= “Ethiopian sidamo”
Coffee[1]= ”kenyan”
Coffee[2]= “café Verona”
Coffee[3]= “Sumatra”
Coffee[4]= “costa rice”
Coffee[5]= “Columbian”
Coffee[6]= “bristan”
The new operator an array
object, and the statements following fill the array with data elements.
An alternative way to define
an array object is to specify the data elements as parameters of the new call.
For example, the following line is the functional equivalent of the previous
example:
Var coffee=new Array(7)
Notice that you did not
specify a size of the array , as is common place in many programming languages.
Javascript does not require that you specify the size of the array, which
allows you to incrementally expand the array as you add each new data element.
However, if you want to, you can specify the size of the array initially as a parameter
in the new expression. Alternatively , you could also resize an array simply by
defining a data element at the n position. If n is the highest number defined
in the array, then the new array size is expanded to n+1.
Example
Var javaDrinks=new
Array()
javaDrinks[0]=”Regular
Coffee”
javaDrinks[1]=”Decaf
Coffee”
javaDrinks[2]=”café
Mocha”
javaDrinks[3]=”café
Lait”
javaDrinks[4]=”café
latte”
The size of the javaDrinks
array is 200, even though only five data elements are defined. Each undefined
elements returns a null value if you access it.
You can retrieve the size of
the array using the Array object’s length property. For example , if you wanted
to iterate through each element in an array object, you could use the script
shown in listing
<script language=”Javascript”>
Var coffee=new Array();
document.write(“coffees of
the world:<p>”)
for(var
i=0;i<coffee.length;i++)
{
document.write(i+1+”. ”+coffee[i]+”<p>”)
}
</script>

No comments:
Post a Comment