1.Array.from用於將類似數組的對象和可遍歷的對象轉爲真正的數組。
Array.from('hello') //['h','e','l','l','0']
Array.from([1,2,3],(x)=>x*x) //[1,4,9]
2.Array.of將一組值轉化爲數組
Array.of(3,11,8) //[3,11,8]
Array.of(3) //[3]
3.數組實例的copyWithin(),在當前數組內部,將指定位置的成員複製到其他位置(覆蓋原有成員),返回當前被修改後的數組
它接受三個參數。
- target(必需):從該位置開始替換數據。
- start(可選):從該位置開始讀取數據,默認爲0。如果爲負值,表示倒數。
- end(可選):到該位置前停止讀取數據,默認等於數組長度。如果爲負值,表示倒數。
[1,2,3,4,5].copyWithin(0,3) //[4,5,3,4,5]
[1,2,3,4,5].copyWithin(0,3,4) //[4,2,3,4,5]
4.數組實例的find方法,用於找出第一個符合條件的數組成員。findIndex返回第一個符合條件的數組成員的位置。
[1,4,-5,10].find((n)=>n<0) //-5
[1,2,3,4,5].findIndex(function(value,index,arr){return value>3}) //3
5.數組實例的fill方法使用給定值,填充一個數組,第二和第三個參數用於指定起始位置和結束位置
['a','b','c'].fill(7) //[7,7,7]
['a','b','c'].fill(7,1,2) //['a',7,'b']
6.數組實例entries(),keys(),values()用於遍歷數組。可以用for...of循環進行遍歷,唯一的區別是keys()是對鍵名的遍歷、values()是對鍵值的遍歷,entries()是對鍵值對的遍歷
for(let index of ['a','b'].keys()){
console.log(index)
}
//0
//1
for(let elem of ['a','b'].values()){
console.log(elem)
}
//'a'
//'b'
for(let [index,elem] of ['a','b'].entries()){
console.log(index,elem)
}
//0 'a'
//1 'b'
如果不使用for...of循環,可以手動調用遍歷器對象的next方法,進行遍歷let letter=['a','b','c'];
let entries=letter.entries();
console.log(entries.next().value);//[0,'a']
console.log(entries.next().value);//[1,'a']
console.log(entries.next().value);//[2,'b']
7.數組實例的includes()返回布爾值,表示某個數組是否包含給定的值,更加語義化。[1,2,3]includes(1); //true
[1,2,3]includes(4); //false
[1,2,NAN]includes(NAN); //true