JS參考型別(2)——Array類型

來源:互聯網
上載者:User

標籤:

【2】Array類型
ECMAScript數組的每一項可以儲存任何類型的資料,並且大小是可以動態調整的,即可以隨著資料的添加自動成長以容納新增資料。

(1)建立數組

1》使用Array建構函式

var colors = new Array();var colors = new Array(20);var colors = new Array("red","blue","green");var colors = Array(3);var colors = Array("Greg");

2》使用數組字面量

var colors = ["red","blue","green"];var colors = [];var colors = [1,2,];//不推薦使用,會建立一個包含2或3項的數組var colors = [,,,,,];//不推薦使用,會建立一個包含5或6項的數組

與對象一樣,使用數組字面量標記法時,也不會調用Array建構函式。

(2)數組length屬性

數組的length屬性不只是可讀的。通過設定這個屬性,可以從數組的末尾移除項或向數組中添加新項。

var colors = ["red","blue","green"];colors.length = 2; //移除最後一項alert(colors[2]); //undefinedvar colors = ["red","blue","green"];colors.length = 4; //數組不存在位置3alert(colors[4]); //undefinedvar colors = ["red","blue","green"];colors[colors.length] = "black"; //在位置3添加一種顏色colors[colors.length] = "brown"; //在位置4添加一種顏色
(3)檢測數組

1》instanceof操作符:確定某個對象是不是數組

if(value instanceof Array){//對數組執行某些操作}

2》Array.isArray()方法:確定某個值到底是不是數組

if(Array.isArray(value)){//對數組執行某些操作}

IE 9+和其他瀏覽器支援此方法。

(4)轉換方法

1》前文講到Object對象都有toLocalString()、toString()、valueOf()方法

  • toLocaleString()方法:返回對象的字串表示,該字串與執行環境的地區對應。
  • toString()方法:返回對象的字串表示。
  • valueOf()方法:返回對象的原始值,可能是字串、數值或布爾值等。通常與toString()方法的傳回值相同。

對數組而言:

  • toString()方法:返回由數組中每個值的字串形式拼接而成的一個以逗號分隔的字串。
  • valueOf()方法:返回對象的原始值。
var colors = ["red","blue","green"];console.log(colors.toString()); //red,blue,greenconsole.log(colors.toLocalString()); //red,blue,greenconsole.log(colors.valueOf()); //["red","blue","green"]console.log(colors); //["red","blue","green"]alert(colors.toString()); //red,blue,greenalert(colors.toLocalString()); //red,blue,greenalert(colors.valueOf()); //red,blue,greenalert(colors); //red,blue,green

alert()要接受字串參數,所以會在後台調用toString()方法。
2》join()方法:使用不同分隔字元來構建包含所有數組項的字串

var colors = ["red","blue","green"];alert(colors.join(",")); //red,blue,greenalert(colors.join("||")); //red||blue||green

如果不給join方法傳入任何值,或者給它傳入undefined,則使用逗號作為分隔字元。
如果數組中某一項的值是null或undefined,那麼該值在join()、toLocalString()、toString()、valueOf()方法返回的結果中以Null 字元串表示。

(5)棧方法:LIFO後進先出;在棧的頂部插入,移除

1》push()方法:可以接受任意數量的參數,把它們逐個添加到數組末尾,並返回修改後數組的長度。
2》pop()方法:從數組末尾移除最後一項,減少數組的length值,然後返回移除的項。

var colors = new Array();var count = colors.push("red","green");alert(count); //2count = colors.push("blue");alert(count); //3var item = colors.pop();alert(item); //"blue"alert(colors.length); //2
(6)隊列方法:FIFO先進先出;隊列在列表的末端添加項,從列表的前端移除項

1》push()方法:可以接受任意數量的參數,把它們逐個添加到數組末尾,並返回修改後數組的長度。
2》shift()方法:移除數組中的第一項並返回該項,同時將數組長度減1。

var colors = new Array();var count = colors.push("red","green");alert(count); //2count = colors.push("blue");alert(count); //3var item = colors.shift();alert(item); //"red"alert(colors.length); //2

3》unshift()方法:在數組前端添加任意個項並返回新數組的長度。

使用unshift()和pop()方法,可以從相反方向來類比隊列,即在數組的前端添加項,從數組末端移除項。

var colors = new Array();var count = colors.unshift("red","green");alert(count); //2count = colors.unshift("blue");alert(count); //3console.log(colors); //["blue","red","green"]var item = colors.pop();alert(item); //"green"alert(colors.length); //2
(7)重排序方法

1》reverse()方法:反轉數組項順序。

var values = [1,2,3,4,5];values.reverse();console.log(values); //[5,4,3,2,1]

2》sort()方法:按升序排列數組項。

var values = [0,1,5,10,15,6,25];values.sort(); //[0,1,10,15,25,5,6] 進行字串比較

sort()方法可以接受一個比較函數作為參數。

function compare1(value1,value2){if(value1<value2){return -1;}else if(value1>value2){return 1;}else{return 0;}}function compare2(value1,value2){if(value1>value2){return -1;}else if(value1<value2){return 1;}else{return 0;}}function compare(value1,value2){return value2 - value1;}var values = [0,1,5,10,15,6,25];values.sort(compare1); //[0,1,5,6,10,15,25] values.sort(compare2); //[25,15,10,6,5,1,0] values.sort(compare); //[25,15,10,6,5,1,0] 
(8)操作方法

1》concat()方法:基於當前數組中的所有項建立一個新數組。不會影響原始數組。

var colors = ["red","green","black"];var colors2 = colors.concat("yellow",["blue","brown"]);alert(colors); //red,green,blackalert(colors2); //red,green,black,yellow,blue,brown

2》slice()方法:基於當前數組中的一或多個項建立一個新數組。不會影響原始數組。
可以接受一或兩個參數,即要返回項的起始和結束位置。

var colors = ["red","green","black","yellow","blue","brown"];var colors2 = colors.slice(1);var colors3 = colors.slice(1,4);alert(colors); //red,green,black,yellow,blue,brownalert(colors2); //green,black,yellow,blue,brownalert(colors3); //green,black,yellow

如果slice()方法的參數中有一個負數,則用數組長度加上該數來確定相應的位置。如果結束位置小於起始位置,則返回空數組。
3》splice()方法。
刪除:可以刪除任意數量的項,只需指定2個參數:要刪除的第一項的位置和要刪除的項數。
插入:可以向指定位置插入任意數量的項,只需提供3個參數:起始位置、0(要刪除的項數)、要插入的項。如果要插入多個項,可以再傳入第四、第五,以至任意多個項。
替換:可以向指定位置插入任意數量的項,且同時刪除任意數量的項,只需提供3個參數:起始位置、要刪除的項數、要插入的任意數量的項。插入的項數不必與刪除的項數相等。
splice()方法始終都會返回一個數組,該數組包含從原始數組中刪除的項(如果沒有刪除任何項,則返回一個空數組)。

var colors = ["red","green","blue"];var removed = colors.splice(0,1);alert(colors); //green,bluealert(removed); //redremoved = colors.splice(1,0,"yellow","orange");alert(colors); //green,yellow,orange,bluealert(removed); // 返回的是一個空數組removed = colors.splice(1,1,"red","purple");alert(colors); //green,red,purple,orange,bluealert(removed); //yellow
(9)位置方法

indexOf()和lastIndexOf()兩個方法。都接收兩個參數:要尋找的項和表示尋找起點位置的索引(可選的)。其中indexOf()方法從數組的開頭開始向後尋找,lastIndexOf()方法從數組的末尾開始向前尋找。
這兩個方法都返回要尋找的項在數組中的位置,或者在沒找到的情況下返回-1。在比較第一個參數與數組中的每一項時,會使用全等操作符,也就是說,要求尋找的項必須嚴格相等。

var numbers = [1,2,3,4,5,4,3,2,1];alert(numbers.indexOf(4)); //3alert(numbers.lastIndexOf(4)); //5alert(numbers.indexOf(4,4)); //5alert(numbers.lastIndexOf(4,4)); //3var person = {name:"Nicholas"};var people = [{name:"Nicholas"}];var morePeople = [person];alert(people.indexOf(person)); //-1alert(morePeople.indexOf(person)); //0
(10)迭代方法

ECMAScript5為數組定義了5個迭代方法。
每個方法都接受兩個參數:要在每一項上啟動並執行函數和(可選的)運行該函數的範圍對象——影響this的值。
傳入這些方法中的函數會接收三個參數:數組項的值、該項在數組中的位置和數組對象本身。

  • every():對數組中的每一項運行給定函數,如果該函數對每一項都返回true,則返回true。
  • some():對數組中的每一項運行給定函數,如果該函數對任一項返回true,則返回true。
  • filter():對數組中的每一項運行給定函數,返回該函數會返回true的項組成的數組。
  • forEach():對數組中的每一項運行給定函數,這個方法沒有傳回值。
  • map():對數組中的每一項運行給定函數,返回每次函數調用的結果組成的數組。

以上方法都不會修改數組中的包含的值。

var numbers = [1,2,3,4,5,4,3,2,1];var everyResult = numbers.every(function(item,index,array){return (item>2);});alert(everyResult); //falsevar someResult = numbers.some(function(item,index,array){return (item>2);});alert(someResult); //truevar filterResult = numbers.filter(function(item,index,array){return (item>2);});alert(filterResult); //[3,4,5,4,3]var mapResult = numbers.map(function(item,index,array){return item*2;});alert(mapResult); //[2,4,6,8,10,8,6,4,2]var forEachResult = numbers.forEach(function(item,index,array){//執行某些操作});//類似於for迴圈

IE 9+和其他瀏覽器支援

(11)歸併方法

reduce()和reduceRight()方法迭代數組所有項,然後構建一個最終返回的值。
其中,reduce()從數組的第一項開始,逐個遍曆到最後。reduceRight()方法則相反。
這兩個方法都接受兩個參數:一個在每一項上調用的函數和(可選的)作為歸併基礎的初始值。
傳入這些方法中的函數會接收四個參數:前一個值,當前值,項的索引和數組對象。

var values = [1,2,3,4,5];var sum = values.reduce(function(prev,cur,index,array){return prev+cur;});alert(sum); //15

IE 9+和其他瀏覽器支援

JS參考型別(2)——Array類型

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.