數組是從零索引,值的有序列表。數組是一個簡便的方式來存儲一組相同類型的有關項(例如字符串),但實際上,一個數組可以包含多個類型的項,甚至是其他數組。
創建一個數組,可以使用數組構造函數或者字面量聲明式,在聲明后,可以賦給變量一系列的值。
~~~
// A simple array with constructor.
var myArray1 = new Array( "hello", "world" );
// Literal declaration, the preferred way.
var myArray2 = [ "hello", "world" ];
~~~
字面量聲明式通常是更好的選擇。查看[谷歌編碼指南](http://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml#Array_and_Object_literals)可獲得更多信息。
如果值是未知的,也可以創建一個空的數組,然后通過數組方法或者訪問索引來添加元素:
~~~
// Creating empty arrays and adding values
var myArray = [];
// Adds "hello" on index 0
myArray.push( "hello" );
// Adds "world" on index 1
myArray.push( "world" );
// Adds "!" on index 2
myArray[ 2 ] = "!";
~~~
`.push()`?是一個函數,它擴展數組并添加一個元素到尾端。您也可以直接通過索引添加項。缺失的指數項將會被?`undefined`?填充。
~~~
// Leaving indices
var myArray = [];
myArray[ 0 ] = "hello";
myArray[ 1 ] = "world";
myArray[ 3 ] = "!";
console.log( myArray ); // [ "hello", "world", undefined, "!" ];
~~~
如果數組的大小是未知的,`.push()`?是更安全的。您可以通過索引取值或者賦值給數組項。
~~~
// Accessing array items by index
var myArray = [ "hello", "world", "!" ];
console.log( myArray[ 2 ] ); // "!"
~~~
## 數組方法和屬性
### .length
`.length`?屬性用于確定數組項的數量。
~~~
// Length of an array
var myArray = [ "hello", "world", "!" ];
console.log( myArray.length ); // 3
~~~
您將需要?`.length`?屬性用于遍歷一個數組:
~~~
// For loops and arrays - a classic
var myArray = [ "hello", "world", "!" ];
for ( var i = 0; i < myArray.length; i = i + 1 ) {
console.log( myArray[ i ] );
}
~~~
### .concat()
通過?`.concat()`?串聯兩個或多個數組:
~~~
var myArray = [ 2, 3, 4 ];
var myOtherArray = [ 5, 6, 7 ];
var wholeArray = myArray.concat( myOtherArray ); // [ 2, 3, 4, 5, 6, 7 ]
~~~
### .join()
`.join()`?使用一個分隔字符拼接數組的所有元素并創建數組的字符串表示。如果沒有提供分隔符(即不帶參數調用?`.join()`),數組會使用逗號進行拼接。
~~~
// Joining elements
var myArray = [ "hello", "world", "!" ];
// The default separator is a comma.
console.log( myArray.join() ); // "hello,world,!"
// Any string can be used as separator...
console.log( myArray.join( " " ) ); // "hello world !";
console.log( myArray.join( "!!" ) ); // "hello!!world!!!";
// ...including an empty one.
console.log( myArray.join( "" ) ); // "helloworld!"
~~~
### .pop()
`.pop()`?移除數組的最后一個元素。它是?`.push()`?的對立方法:
~~~
// Pushing and popping
var myArray = [];
myArray.push( 0 ); // [ 0 ]
myArray.push( 2 ); // [ 0 , 2 ]
myArray.push( 7 ); // [ 0 , 2 , 7 ]
myArray.pop(); // [ 0 , 2 ]
~~~
### .reverse()
顧名思義,調用?`.reverse()`?方法后,數組中的元素按相反的順序排列:
~~~
var myArray = [ "world" , "hello" ];
myArray.reverse(); // [ "hello", "world" ]
~~~
### .shift()
移除數組中的第一個元素。結合?`.push`?和?`.shift()`,你可以重建一個[隊列](http://zh.wikipedia.org/wiki/%E9%98%9F%E5%88%97)方法:
~~~
// Queue with shift() and push()
var myArray = [];
myArray.push( 0 ); // [ 0 ]
myArray.push( 2 ); // [ 0 , 2 ]
myArray.push( 7 ); // [ 0 , 2 , 7 ]
myArray.shift(); // [ 2 , 7 ]
~~~
### .slice()
提取數組的一部分,并返回一個包含該部分的新數組。這個方法需要一個參數,起始的索引:
~~~
// Slicing
var myArray = [ 1, 2, 3, 4, 5, 6, 7, 8 ];
var newArray = myArray.slice( 3 );
console.log( myArray ); // [ 1, 2, 3, 4, 5, 6, 7, 8 ]
console.log( newArray ); // [ 4, 5, 6, 7, 8 ]
~~~
`.slice()`?方法有一個可選的第二個參數,結束的索引。
~~~
console.log( [ 1, 2, 3, 4, 5, 6, 7, 8 ].slice( 2, 5 ) ); // [ 3, 4, 5 ]
~~~
### .splice()
移除一個確定數量的元素并在給定的索引處開始添加新的元素。它至少需要三個參數:
~~~
myArray.splice( index, length, values, ... );
~~~
* _Index_?– 開始的索引。
* _Length_?– 移除的元素數量。
* _Values_?– 在索引的位置插入的值。
例如:
~~~
var myArray = [ 0, 7, 8, 5 ];
myArray.splice( 1, 2, 1, 2, 3, 4 );
console.log( myArray ); // [ 0, 1, 2, 3, 4, 5 ]
~~~
### .sort()
數組排序。它需要一個參數,一個比較函數。如果沒有提供這個函數,數組默認按照升序進行排序:
~~~
// Sorting without comparing function.
var myArray = [ 3, 4, 6, 1 ];
myArray.sort(); // 1, 3, 4, 6
~~~
~~~
// Sorting with comparing function.
function descending( a, b ) {
return b - a;
}
var myArray = [ 3, 4, 6, 1 ];
myArray.sort( descending ); // [ 6, 4, 3, 1 ]
~~~
例子中的?`descending`?函數返回的值很重要。如果返回的值小于0,`a`?的位置在?`b`之前,如果值大于0則位置相反。如果值等于0,則元素的位置(與當前)相同。
### .unshift()
在數組的第一個位置插入一個元素:
~~~
var myArray = [];
myArray.unshift( 0 ); // [ 0 ]
myArray.unshift( 2 ); // [ 2 , 0 ]
myArray.unshift( 7 ); // [ 7 , 2 , 0 ]
~~~
### .forEach()
在現代瀏覽器中可以使用?`.forEach()`?方法遍歷數組,您傳遞個這個方法的函數會被數組中的每個元素調用。
被傳遞的函數可以帶三個參數:
* _Element_?– 元素本身。
* _Index_?– 元素在數組中的索引。
* _Array_?– 數組本身。
所有的參數都是可選的,但你通常至少需要一個?_Element_?參數。
~~~
// Native .forEach()
function printElement( elem ) {
console.log( elem );
}
function printElementAndIndex( elem, index ) {
console.log( "Index " + index + ": " + elem );
}
function negateElement( elem, index, array ) {
array[ index ] = -elem;
}
myArray = [ 1, 2, 3, 4, 5 ];
// Prints all elements to the console
myArray.forEach( printElement );
// Prints "Index 0: 1", "Index 1: 2", "Index 2: 3", ...
myArray.forEach( printElementAndIndex );
// myArray is now [ -1, -2, -3, -4, -5 ]
myArray.forEach( negateElement );
~~~