Visit Mozilla.org

Core JavaScript 1.5 Guide:Predefined Core Objects:Objeto Array

De MDC

Tabla de contenidos

[editar] Array Object

JavaScript does not have an explicit array data type. However, you can use the predefined Array object and its methods to work with arrays in your applications. The Array object has methods for manipulating arrays in various ways, such as joining, reversing, and sorting them. It has a property for determining the array length and other properties for use with regular expressions.

An array is an ordered set of values that you refer to with a name and an index. For example, you could have an array called emp that contains employees' names indexed by their employee number. So emp[1] would be employee number one, emp[2] employee number two, and so on.


[editar] Creating an Array

To create an Array object:

1. arrayObjectName = new Array(element0, element1, ..., elementN)
2. arrayObjectName = new Array(arrayLength)

arrayObjectName is either the name of a new object or a property of an existing object. When using Array properties and methods, arrayObjectName is either the name of an existing Array object or a property of an existing object.

element0, element1, ..., elementN is a list of values for the array's elements. When this form is specified, the array is initialized with the specified values as its elements, and the array's length property is set to the number of arguments.

arrayLength is the initial length of the array. The following code creates an array of five elements:

billingMethod = new Array(5)

Array literals are also Array objects; for example, the following literal is an Array object. See Array Literals for details on array literals.

coffees = ["French Roast", "Columbian", "Kona"]

[editar] Populating an Array

You can populate an array by assigning values to its elements. For example,

emp[1] = "Casey Jones"
emp[2] = "Phil Lesh"
emp[3] = "August West"

You can also populate an array when you create it:

myArray = new Array("Hello", myVar, 3.14159)

[editar] Referring to Array Elements

You refer to an array's elements by using the element's ordinal number. For example, suppose you define the following array:

myArray = new Array("Wind","Rain","Fire")

You then refer to the first element of the array as myArray[0] and the second element of the array as myArray[1].

The index of the elements begins with zero (0), but the length of array (for example, myArray.length) reflects the number of elements in the array.


[editar] Array Methods

The Array object has the following methods:

  • concat joins two arrays and returns a new array.

   myArray = new Array("1","2","3")
   myArray = myArray.concat("a", "b", "c"); // myArray is now ["1", "2", "3", "a", "b", "c"]

  • join(deliminator = ",") joins all elements of an array into a string.

   myArray = new Array("Wind","Rain","Fire")
   list = myArray.join(" - "); // list is "Wind - Rain - Fire"

  • pop removes the last element from an array and returns that element.

   myArray = new Array("1", "2", "3");
   last=myArray.pop(); // MyArray is now ["1", "2"], last = "3"

  • push adds one or more elements to the end of an array and returns that last element added.

   myArray = new Array("1", "2");
   myArray.push("3"); // MyArray is now ["1", "2", "3"]

  • reverse transposes the elements of an array: the first array element becomes the last and the last becomes the first.

   myArray = new Array ("1", "2", "3");
   myArray.reverse(); // transposes the array so that myArray = [ "3", "2", "1" ]

  • shift removes the first element from an array and returns that element

   myArray = new Array ("1", "2", "3");
   first=myArray.shift(); // MyArray is now ["2", "3"], first is "1"

  • slice (start_index, upto_index) extracts a section of an array and returns a new array.

   myArray = new Array ("a", "b", "c", "d", "e");
   myArray = myArray.slice(1,4); //starts at index 1 and extracts all elements until index 4, returning [ "b", "c", "d" ]

  • splice(index, count_to_remove, addelement1, addelement2, ...) adds and/or removes elements from an array.

   myArray = new Array ("1", "2", "3", "4", "5");
   myArray.splice(1,3,"a","b","c", "d"); // MyArray is now ["1", "a", "b", "c", "d", "5"]
   // this code started at index one (or where the "2" was), removed 3 elements there, and then inserted all consecutive elements in its place

  • sort sorts the elements of an array.

   myArray = new Array("Wind","Rain","Fire")
   myArray.sort(); // sorts the array so that myArrray = [ "Fire", "Rain", "Wind" ]

sort can also take a callback function to determine how array content is sorted. The function compares two values and returns one of three values:

  • if a is less than b by the sorting system, return -1 (or any negative number)
  • if a is greater than b by the sorting system, return 1 (or any positive number)
  • if a and b is considered equivalent, return 0.

For, instance, the following will support by the last letter of an array:

   var sortFn = function(a,b){
       if (a[a.length - 1] < b[b.length - 1]) return -1;
       if (a[a.length - 1] > b[b.length - 1]) return 1;
       if (a[a.length - 1] == b[b.length - 1]) return 0;
       }
   myArray.sort(sortFn); // sorts the array so that myArray = ["Wind","Fire","Rain"]

  • unshift adds one or more elements to the front of an array and returns the new length of the array.

[editar] Two-Dimensional Arrays

The following code creates a two-dimensional array.

a = new Array(4)
for (i=0; i < 4; i++) {
   a[i] = new Array(4)
   for (j=0; j < 4; j++) {
      a[i][j] = "["+i+","+j+"]"
   }
}

This example creates an array with the following rows:

Row 0:[0,0][0,1][0,2][0,3]
Row 1:[1,0][1,1][1,2][1,3]
Row 2:[2,0][2,1][2,2][2,3]
Row 3:[3,0][3,1][3,2][3,3]

[editar] Arrays and Regular Expressions

When an array is the result of a match between a regular expression and a string, the array returns properties and elements that provide information about the match. An array is the return value of RegExp.exec, String.match, and String.split. For information on using arrays with regular expressions, see Chapter 4, Regular Expressions.