n JavaScript, the Array property of the global object is a constructor for array instances.(在JavaScript,全局对象数组属性是构造数组对象的构造器。)
An array is a JavaScript object. Note that you shouldn't use it as an associative array, use Object instead.(一个数组是JavaScript对象。注意你不应该把它作为关联数组来使用,应该使用对象来代替。)
Creating an Array(创建数组)
The following example creates an array, msgArray, with a length of 0, then assigns values to msgArray[0] and msgArray[99], changing the length of the array to 100.(下面的例子创建了一个长度为0的数组msgArray,然后把数值赋给msgArray[0]到msgArray[99],把数组的长度改变成100。)
- <script>
- var msgArray = new Array();
- msgArray[0] = "Hello";
- msgArray[99] = "world";
- if (msgArray.length == 100){
- document.write("The length is 100.");
- }
- </script>
Creating a Two-dimensional Array(创建一个二维数组)
The following creates chess board as a two dimensional array of strings.(下面的例子创建了二维字符串数组来表示国际象棋棋盘) The first move is made by copying the 'P' in 6,4 to 4,4. The position 6,4 is left blank.(第一步是拷贝6,4的'P'到4,4。位置6,4是空白的)
- <script>
- var board =
- [ ['R','N','B','Q','K','B','N','R'],
- ['P','P','P','P','P','P','P','P'],
- [' ',' ',' ',' ',' ',' ',' ',' '],
- [' ',' ',' ',' ',' ',' ',' ',' '],
- [' ',' ',' ',' ',' ',' ',' ',' '],
- [' ',' ',' ',' ',' ',' ',' ',' '],
- ['p','p','p','p','p','p','p','p'],
- ['r','n','b','q','k','b','n','r']];
- document.write(board.join('\n') + '\n\n' + "<br/>");
- // Move King's Pawn forward 2
- board[4][4] = board[6][4];
- board[6][4] = ' ';
- document.write(board.join('\n'));
- </script>
Here is the output:(下面是输出:)
R,N,B,Q,K,B,N,R
P,P,P,P,P,P,P,P
, , , , , , ,
, , , , , , ,
, , , , , , ,
, , , , , , ,
p,p,p,p,p,p,p,p
r,n,b,q,k,b,n,r
R,N,B,Q,K,B,N,R
P,P,P,P,P,P,P,P
, , , , , , ,
, , , , , , ,
, , , ,p, , ,
, , , , , , ,
p,p,p,p, ,p,p,p
r,n,b,q,k,b,n,r
Accessing array elements(访问数组元素)
Array elements are nothing less than object properties, so they are accessed as such.(数组元素和对象属性是一样的,所以它们是这样被访问的。)
- <script>
- var myArray = new Array("Wind", "Rain", "Fire");
- document.write(myArray[0] + "<br/>"); // "Wind"
- document.write(myArray[1] + "<br/>"); // "Rain"
- // etc.
- document.write(myArray.length + "<br/>"); // 3
- // Even if indices are properties, the following notation throws a syntax error(即使索引是属性,下面符号将会抛出语法错误)
- //document.write(myArray.2 + "<br/>");
- // It should be noted that in JavaScript, object property names are strings. Consequently,
- //应该注意的是,在JavaScript中,对象的属性名称是字符串。因此,
- document.write((myArray[0] === myArray["0"]) + "<br/>");
- document.write((myArray[1] === myArray["1"]) + "<br/>");
- // etc.
- // However, this should be considered carefully(无论如何,这都要仔细地思考)
- document.write(myArray[02] + "<br/>"); // "Fire". The number 02 is converted as the "2" string
- document.write(myArray["02"] + "<br/>"); // undefined. There is no property named "02"
- </script>
Relationship between length and numerical properties(长度和数值属性们的关系)
An array's length property and numerical properties are connected. (数组长度属性和数值属性是关联的)Here is some code explaining how this relationship works.(下面这些代码解释了这些关系是怎么工作的。)
- <script>
- var a = [];
- a[0] = 'a';
- console.log(a[0]); // 'a'
- console.log(a.length); // 1
- a[1] = 32;
- console.log(a[1]); // 32
- console.log(a.length); // 2
- a[13] = 12345;
- console.log(a[13]); // 12345
- console.log(a.length); // 14
- a.length = 10;
- console.log(a[13]); // undefined, when reducing the length elements after length+1 are removed
- console.log(a.length); // 10
- </script>
Creating an array using the result of a match(使用match的结果创建对象)
The result of a match between a regular expression and a string can create an array. (正则表达式和字符串的match的结果可以创建一个字符串。)This array has properties and elements that provide information about the match.(这个数组有能提供关于这个match的信息的属性和元素。) An array is the return value of RegExp.exec, String.match, and String.replace.(RegExp.exec,String.match,String.replace返回了数组) To help explain these properties and elements, look at the following example and then refer to the table below:(为了解释这些属性和元素,请看下面的例子,接下来看看下面的表格:)
- // Match one d followed by one or more b's followed by one d
- // Remember matched b's and the following d
- // Ignore case
- var myRe = /d(b+)(d)/i;
- var myArray = myRe.exec("cdbBdbsbz");
111