Array構(gòu)造函數(shù)
當(dāng)Array被當(dāng)做一個函數(shù)調(diào)用時,也會創(chuàng)建并初始化一個新的Array對象。因此,當(dāng)參數(shù)相同時,函數(shù)調(diào)用Array(...)等價于創(chuàng)建一個new Array(...)的表達式。
創(chuàng)建數(shù)組
1.Array()
//創(chuàng)建數(shù)組
var color = new Array();
2.Array ( len )
//創(chuàng)建長度為20的數(shù)組
var color = new Array(20);
//等價于
var color = Array(20);
3.Array ( ...items )
//創(chuàng)建長度為20的數(shù)組
0
//等價于
var color = Array("red", "blue", "green");
通常我們使用數(shù)組字面量表示法來創(chuàng)建數(shù)組。數(shù)組字面量由一對包含數(shù)組的[]表示,多個數(shù)組項之間用“,”分隔。
var color = ["red", "blue", "green"]; //創(chuàng)建一個包含3項的數(shù)組
var names = []; //創(chuàng)建一個空數(shù)組
Array構(gòu)造函數(shù)的屬性
1.Array.from ( items [ , mapfn [ , thisArg ] ] )
Array.from()用于將兩類對象轉(zhuǎn)換未真正的數(shù)組:類似數(shù)組的對象(array-like object)和可遍歷(iterable)的對象,其中包括ES6新增的Set和Map結(jié)構(gòu)。
let ps = document.querySelectorAll("p");
Array.from(ps).forEach(function(p){
console.log(p);
});
上面的代碼中querySelectorAll方法返回的是一個類似數(shù)組的對象,只有將這個對象轉(zhuǎn)換成真正的數(shù)組,才能使用forEach方法。
Array.from()還可以接受第二個參數(shù),作用類似于數(shù)組的map方法,用來對每個元素進行處理。
Array.from(arrayLike, x => x * x);
//等同于
Array.from(arrayLike).map( x => x * x);
2.Array.isArray ( arg )
Array.isArray() 方法用來判斷某個值是否為 數(shù)組。返回布爾類型。
3.Array.of ( ...items )
Array.of()用于將一組數(shù)值轉(zhuǎn)換為數(shù)組
這個函數(shù)的主要目的,是彌補數(shù)組函數(shù)Array()的不足。因為參數(shù)個數(shù)的不同會導(dǎo)致Array()的行為有差異。
Array.of(3, 11, 8); //[3,11,8]
Array.of(3),length; //1
Array(3); // [undefined, undefined, undefined]
Array(3, 11, 8); //[3,11,8]
Array構(gòu)造函數(shù)的屬性
1.Array.prototype.concat ( ...arguments )
concat()方法可以基于當(dāng)前數(shù)組中的所有項創(chuàng)建一個新的數(shù)組。具體來說,這個方法會先創(chuàng)建當(dāng)前數(shù)組一個副本,然后將接收到的參數(shù)添加到這個副本的末尾,最后返回新構(gòu)建的數(shù)組。
var colors = ["red", "blue", "green"];
var colors2 = colors.concat("yellow", ["black", "brown"]);
alert(colors); //red,blue,green
alert(colors2); //red,blue,green,yellow,black,brown
2.Array.prototype.copyWithin ( target, start [ , end ] )
copyWithin方法,在當(dāng)前數(shù)組內(nèi)部,將指定位置的成員復(fù)制到其他位置(會覆蓋原有成員),然后返回當(dāng)前數(shù)組。
它接受三個參數(shù)。
target (必需):從該位置開始替換數(shù)據(jù)。
start (可選):從該位置開始讀取數(shù)據(jù),默認為 0 。如果為負值,表示倒數(shù)。
end (可選):到該位置前停止讀取數(shù)據(jù),默認等于數(shù)組長度。如果為負值,表示倒數(shù)。
// 將 3 號位復(fù)制到 0 號位
[1, 2, 3, 4, 5].copyWithin(0, 3, 4) // [4,2,3,4,5]
// -2 相當(dāng)于 3 號位, -1 相當(dāng)于 4 號位
[1, 2, 3, 4, 5].copyWithin(0, -2, -1) // [4,2,3,4,5]
3.Array.prototype.entries ( )
entries() 方法用于遍歷數(shù)組。返回一個 Array Iterator 對象,該對象包含數(shù)組中每一個索引的鍵值對。
for(let [index, elem] of ["a", "b"].entries()){
console.log(index, elem);
}
// 0 "a"
// 1 "b"
4.Array.prototype.every/some ( callbackfn [ , thisArg ] )
every()對數(shù)組中的每一項運行給定函數(shù),如果該函數(shù)對每一項都返回true,則返回true。some()如果有一項返回true,則返回true。
var number = [1, 2, 3, 4, 5, 4, 3, 2, 1];
var everyResult = number.every(function(item, index, array){
return (item > 2)
});
alert(everyResult); //false
var number = [1, 2, 3, 4, 5, 4, 3, 2, 1];
var someResult = number.some(function(item, index, array){
return (item > 2)
});
alert(someResult); //true
5.Array.prototype.fill ( value [ , start [ , end ] ] )
fill()使用給定值填充一個數(shù)組。fill()還可以 接收第二個和第三個參數(shù),用于指定填充的起始位置和結(jié)束位置。
["a", "b", "c"].fill(7); //[7,7,7]
new Array(3).fill(7); //[7,7,7]
["a", "b", "c"].fill(7, 1, 2); //['a',7,'c']
6.Array.prototype.filter ( callbackfn [ , thisArg ] )
filter()對數(shù)組中的每一項運行給定函數(shù),返回該函數(shù)會返回true的項組成的數(shù)組。
var number = [1, 2, 3, 4, 5, 4, 3, 2, 1];
var filterResult = number.filter(function(item, index, array){
return (item > 2)
});
alert(filterResult); //3,4,5,4,3
7Array.prototype.find ( predicate [ , thisArg ] )
find()用于找出數(shù)組中第一個符合條件的數(shù)組元素。它的參數(shù)是一個回調(diào)函數(shù),所有的數(shù)組元素一次遍歷該回調(diào)函數(shù),直到找出第一個返回值為true的元素,然后返回該元素,否則返回undefined。
[1, 5, 10, 15].find(function(value, index, arr){
return value > 9;
}) //10
8.Array.prototype.findIndex ( predicate [ , thisArg ] )
findIndex()用于找出數(shù)組中第一個符合條件的數(shù)組元素的位置,如果所有元素都不符合條件,返回-1。
[1, 5, 10, 15].findIndex(function(value, index, arr){
return value > 9;
}) //2
9.Array.prototype.forEach ( callbackfn [ , thisArg ] )
forEach()對數(shù)組中的每一項運行給定函數(shù)。這個方法沒有返回值。
var number = [1, 2, 3, 4, 5, 4, 3, 2, 1];
number.forEach(function(item, index, array){
//執(zhí)行某些操作
});
10.Array.prototype.includes ( searchElement [ , fromIndex ] )
includes()用來判斷當(dāng)前數(shù)組是否包含某指定的值,如果是,則返回true,否則false
var a = [1, 2, 3];
a.includes(2); // true
a.includes(4); // false
11.Array.prototype.indexOf /lastIndexOf( searchElement [ , fromIndex ] )
indexOf()和lastIndexOf()返回某個指定的字符串值在字符串中查找起點位置的索引。indexOf()方法從數(shù)組的開頭(位置0)開始向后查找,lastIndexOf()方法則是從數(shù)組末尾開始向前查找。
var number = [1, 2, 3, 4, 5, 4, 3, 2, 1];
alert(number.indexOf(4)); //3
alert(number.lastIndexOf(4)); //5
alert(number.indexOf(4, 4)); //5
alert(number.lastIndexOf(4, 4)); //3
alert(number.indexOf(8)); //-1
12.Array.prototype.join ( separator )
join() 方法用于把數(shù)組中的所有元素放入一個字符串。元素是通過指定的分隔符進行分隔的。
var colors = ["red", "blue", "green"];
alert(color.join(",")); //red,green,blue
alert(color.join("||")); //red||green||blue
如果不給join()方法傳入任何參數(shù),或者給它傳入undefined,則使用“,”作為分隔符。
13.Array.prototype.keys/values( )
keys()是對鍵名的遍歷,values( )是對鍵值的遍歷。
for(let index of ["a", "b"].keys()){
console.log(index);
}
// 0
// 1
for(let elem of ["a", "b"].values()){
console.log(elem);
}
// "a"
// "b"
14.Array.prototype.map ( callbackfn [ , thisArg ] )
map()對數(shù)組中的每一項運行給定函數(shù),返回每次函數(shù)調(diào)用的結(jié)果組成的數(shù)組。
var number = [1, 2, 3, 4, 5, 4, 3, 2, 1];
var mapResult = number.map(function(item, index, array){
return item * 2
});
alert(mapResult); //2,4,6,8,10,8,6,4,2
15.Array.prototype.push/pop ( )
棧是一種LIFO(后進先出)的數(shù)據(jù)結(jié)構(gòu)。而棧中項的插入和移除只發(fā)生在一個位置 - 棧的頂部。ECMAScript為數(shù)組專門提供了push()和pop()方法,以便實現(xiàn)類似棧的行為。push()返回修改后的數(shù)組的長度。pop()返回數(shù)組移除項。
var color = new Array();
var count = color.push("red", "green");
alert(count); //2
count = color.push("black");
alert(count); //3
var item = color.pop();
alert(item); //"balck"
alert(color.length); //2
16.Array.prototype.reduce/reduceRight ( callbackfn [ , initialValue ] )
reduce()和reduceRight()都會迭代數(shù)組的所有項,然后構(gòu)建一個最終返回的值。其中reduce()方法從數(shù)組的第一項開始,逐個遍歷到最后,而reduceRight()則從數(shù)組的最后一項開始,向前遍歷到第一項。兩個方法都接收兩個參數(shù):一個在每一項上調(diào)用的函數(shù)和(可選的)作為歸并基礎(chǔ)的初始值。 傳給reduce()和reduceRight()的函數(shù)接收4個參數(shù):前一個值、當(dāng)前值、項的索引和數(shù)組對象。
var value = [1, 2, 3, 4, 5];
var sum = value.reduce(function(prev, cur, index, array){
return prev + cur;
});
alert(sum); //15
var value = [1, 2, 3, 4, 5];
var sum = value.reduceRight(function(prev, cur, index, array){
return prev + cur;
});
alert(sum); //15
reduceRight()和reduce()作用類似,只是方向相反。執(zhí)行結(jié)果相同
17.Array.prototype.reverse ( )
reverse()會反轉(zhuǎn)數(shù)組項的順序
var values = [1, 2, 3, 4, 5];
console.log(values.reverse()); // [5, 4, 3, 2, 1]
18.Array.prototype.push/shift/unshift ( )
隊列數(shù)據(jù)結(jié)構(gòu)的訪問規(guī)則是FIFO(先進先出)。隊列在列表的末端添加,從列表的前端移除項。shift()能夠移除數(shù)組中的第一項并返回數(shù)組。unshift ()shift()功能相反。和結(jié)合使用shift()和push().
var color = new Array();
var count = color.push("red", "green");
alert(count); //2
count = color.push("black");
alert(count); //3
var item = color.shift();
alert(item); //"red"
alert(color.length); //2
count = color.unshift("yellow");
alert(count ); //3
19.Array.prototype.slice ( start, end )
slice() 方法可從已有的數(shù)組中返回選定的元素。slice() 接收兩個參數(shù),即返回項的起始位置和結(jié)束位置(可選)。
var color = Array("red", "blue", "green", "yellow", "purple");
var color2 = color.slice(1);
var color3 = color.slice(1,4);
alert(color2); // blue,green,yellow,purple
alert(color3); // blue,green,yellow
20.Array.prototype.sort ( comparefn )
sort() 方法對數(shù)組的元素做排序,并返回這個數(shù)組。即使數(shù)組中的每一項都是數(shù)值,sort()方法比較的還是字符串。
var value = [0, 1, 5, 10, 15];
value.sort();
alert(value); //0,1,10,15,5
function sortNumber(a,b)
{
return a - b
}
alert(value.sort(sortNumber)) //0,1,5,10,15
21.Array.prototype.splice ( start, deleteCount, ...items )
splice() 方法向/從數(shù)組中添加/刪除項目,然后返回被刪除的項目。splice() 方法會改變原數(shù)組。
var color = Array("red", "blue", "green");
var removed = coloe.splice(0, 1);
alert(colors); //blue,green
alert(removed); //red
removed = coloe.splice(1, 0, "yellow", "orange");
alert(colors); //blue,yellow,orange,green
alert(removed); //空數(shù)組
removed = coloe.splice(1, 1, "red", "purple");
alert(colors); //blue,red,purple,orange,green
alert(removed); //yellow
22.Array.prototype.toString/toLocaleString ( )
toString() 方法可把數(shù)組轉(zhuǎn)換為字符串,并返回結(jié)果。toLocaleString()把數(shù)組轉(zhuǎn)換為本地字符串。
var color = Array("red", "blue", "green");
alert(color.toString()); //red,blue,green
alert(color.toLocaleString()); //red,blue,green
參考資料
https://tc39.github.io/ecma262/
JavaScript高級程序設(shè)計(第3版)
ECMAScript 6入門