JavaScript Tutorial/Array/Introduction

Материал из Web эксперт
Перейти к: навигация, поиск

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.



   <source lang="javascript">

<HTML> <SCRIPT LANGUAGE="JavaScript">

</SCRIPT> </HTML></source>


Array"s Constructor

Constructors:



   <source lang="javascript">

var variable = new Array() var variable = new Array(int) var variable = new Array(arg1, ..., argN)</source>


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

   <source lang="javascript">

<HTML>

  <HEAD>
  <TITLE>
  Iteration
  </TITLE>
  </HEAD>
  <BODY>

<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] + "
"); } </SCRIPT>

  </BODY>

</HTML></source>


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.



   <source lang="javascript">

<html> <SCRIPT LANGUAGE="JavaScript">

</SCRIPT> </html></source>


Creating an Array and Accessing Its Elements

   <source lang="javascript">

<html>

Creating and Accessing Arrays

   <script language="JavaScript">
   
   </script>
   </html></source>
   
  

Delete an array element

   <source lang="javascript">

<HTML>

  <HEAD>
  <TITLE>
  Iteration
  </TITLE>
  </HEAD>
  <BODY>

<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] + "
"); } </SCRIPT>

  </BODY>

</HTML></source>


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

   <source lang="javascript">

<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>

<SCRIPT> var x = makeArray(); document.write(showArray(x)); </SCRIPT>

  </BODY>

</HTML></source>


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

   <source lang="javascript">

<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>

<SCRIPT> var x = makeArray(); document.write(showArray(x)); </SCRIPT>

  </BODY>

</HTML></source>


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:



   <source lang="javascript">

var x = new Array();</source>