JavaScript Tutorial/Array/Introduction
Содержание
- 1 Arrays as Objects
- 2 Array"s Constructor
- 3 Assign undefined value to an array element
- 4 Create an array using the Object() constructor
- 5 Creating an Array and Accessing Its Elements
- 6 Delete an array element
- 7 Methods Available in the Array Object
- 8 Pass an array to a function
- 9 Properties and Methods Used by the Array Object
- 10 Return an array from a function
- 11 Use Array"s Constructor
Arrays as Objects
It is possible to access the elements of arrays as properties if a string index is used.
The [] operators and dot notation can be used interchangeably when accessing the contents of the array.
<HTML>
<SCRIPT LANGUAGE="JavaScript">
<!--
function populateArray(table) {
table.A=46;
table.B=23;
table["B"]=14;
table["C"]=15;
}
function displayArray(table) {
document.write(table["A"]," A.<BR>");
document.write(table["B"]," B.<BR>");
document.write(table.B," B.<BR>");
document.write(table.C," pairs of C.");
}
var myArray = new Array();
populateArray(myArray);
displayArray(myArray);
-->
</SCRIPT>
</HTML>
Array"s Constructor
Constructors:
var variable = new Array()
var variable = new Array(int)
var variable = new Array(arg1, ..., argN)
These constructors create a new array and initialize the Array object based on the arguments passed in the parameter list.
The constructor that has no arguments sets the length property to 0.
int-- An array is created and its length property is set to the value int.
arg1,...argN--An array is created and the array is populated with the arguments. The array length property is set to the number of arguments in the parameter list.
The newly created array is returned from the constructor.
Assign undefined value to an array element
<HTML>
<HEAD>
<TITLE>
Iteration
</TITLE>
</HEAD>
<BODY>
<H1>
<SCRIPT>
var myArray = new Array(4);
myArray[0] = "A";
myArray[1] = undefined;
myArray[2] = "C";
myArray[3] = "D";
myArray[6] = "E";
delete myArray[2]
for (var i = 0; i < myArray.length; i++){
if (myArray[i] != undefined)
document.write("myArray[" + i + "] = " + myArray[i] + "<br>");
}
</SCRIPT>
</H1>
</BODY>
</HTML>
Create an array using the Object() constructor
A new object is created using the Object() constructor.
Elements the are assigned to the object using the [] operators.
The programmer is responsible for keeping the length of the array.
<html>
<SCRIPT LANGUAGE="JavaScript">
<!--
var myArray = Object();
myArray.length=3; //array position zero
myArray[1]="A";
myArray[2]="B";
myArray[3]="C";
document.write("The myArray holds: ");
for(x=1; x<=myArray.length; x++) {
document.write(myArray[x]," ");
}
-->
</SCRIPT>
</html>
Creating an Array and Accessing Its Elements
<html>
<h2>Creating and Accessing Arrays</h2>
<script language="JavaScript">
<!--
numArray = new Array(4,6,3);
document.write("[0]=",numArray[0],"<br>");
document.write("[1]=",numArray[1],"<br>");
document.write("[2]=",numArray[2]);
//End Hide-->
</script>
</html>
Delete an array element
<HTML>
<HEAD>
<TITLE>
Iteration
</TITLE>
</HEAD>
<BODY>
<H1>
<SCRIPT>
var myArray = new Array(4);
myArray[0] = "A";
myArray[1] = undefined;
myArray[2] = "C";
myArray[3] = "D";
myArray[6] = "E";
delete myArray[2]
for (var i = 0; i < myArray.length; i++){
if (myArray[i] != undefined)
document.write("myArray[" + i + "] = " + myArray[i] + "<br>");
}
</SCRIPT>
</H1>
</BODY>
</HTML>
Methods Available in the Array Object
Method Description join() Concatenates all elements into one string reverse() Reverses the order of the elements in the array sort() Sorts elements in array concat() Concatenates an array on to an array slice() Returns a subsection of the array splice() Inserts and removes elements from an array push() Adds elements to the end of an array pop() Deletes the last element from an array unshift() Adds elements to the front of an array shift() Deletes elements from the front of an array toString() Converts elements to a string toSource() Converts elements to a string with square brackets
Pass an array to a function
<HTML>
<HEAD>
<TITLE>
Iteration Two
</TITLE>
<SCRIPT>
function makeArray() {
var myArray = new Array(4);
myArray[0] = "A";
myArray[1] = "B";
myArray[2] = "C";
myArray[3] = "D";
return myArray;
}
function showArray(theArray){
var quote = "";
for (var i = 0; i < theArray.length; i++){
quote += theArray[i] + " ";
}
return quote;
}
</SCRIPT>
</HEAD>
<BODY>
<H1>
<SCRIPT>
var x = makeArray();
document.write(showArray(x));
</SCRIPT>
</H1>
</BODY>
</HTML>
Properties and Methods Used by the Array Object
Item Description length The number elements in the array concat() Concatenates an array on to an array join() Concatenates all elements of an array into one string pop() Deletes the last element from an array push() Adds elements to the end of an array reverse() Reverses the order of the elements in the array shift() Deletes elements from the front of an array slice() Returns a subsection of the array sort() Sorts elements in array splice() Inserts and removes elements from an array toSource() Converts elements to a string with square brackets toString() Converts elements to a string unshift() Adds elements to the front of an array
Return an array from a function
<HTML>
<HEAD>
<TITLE>
Iteration Two
</TITLE>
<SCRIPT>
function makeArray() {
var myArray = new Array(4);
myArray[0] = "A";
myArray[1] = "B";
myArray[2] = "C";
myArray[3] = "D";
return myArray;
}
function showArray(theArray){
var quote = "";
for (var i = 0; i < theArray.length; i++){
quote += theArray[i] + " ";
}
return quote;
}
</SCRIPT>
</HEAD>
<BODY>
<H1>
<SCRIPT>
var x = makeArray();
document.write(showArray(x));
</SCRIPT>
</H1>
</BODY>
</HTML>
Use Array"s Constructor
Arrays stores multiple data based on a numbered position into one storage structure.
The index starts at 0 and goes up.
JavaScript supports having arrays within arrays, called multidimensional arrays.
One-Dimensional array
To create an instance of an array, you use the new operator along with the Array object.
There are four ways to declare an array.
First, an empty array can be created by leaving the constructor parameters empty:
var x = new Array();