1、Array.of()函數
函數做用:將一組值,轉換成數組
var arr=Array.of(1,2,3,4)
console.log(arr) // [1,2,3,4]
2、Array.from()函數
1.能夠將相似數組的對象或者可遍歷的對象轉換成真正的數組。
好比:getElementsByTagName等
let ele = document.getElementsByTagName('a'); ele instanceof Array; //結果:false,不是數組 ele instanceof Object; //結果:true,是對象
Array.from(ele) instanceof Array; //結果:true,是數組
2.將字符串轉換成數組
let str='hello';
let newarr=Array.from(str)
console.log(newarr) // ['h','e','l','l','o']
3、find()函數
函數做用:找出數組中符合條件的第一個元素。
let arr=[1,3,5,6,7]
let x=arr.find(function(num){
return num<8 // 1
num>2 //3
num>9 // underfined
})
能夠看出 find()函數的參數是一個匿名函數,數組的每一個元素都會進入匿名函數執行,直到結果爲true,find函數就會返回value的值,若是全部元素都不符合匿名函數的條件,find( )函數就會返回undefind。
4、findIndex()函數
函數做用:返回符合條件的第一個數組成員的位置。
let arr=[1,3,5,6,7]
let x=arr.findIndex(function(num){
return num<8 // 0
num>2 // 1
num>9 // -1
})
能夠看出數組元素是從0算起;不符合匿名函數的條件,findIndex( )函數就會返回-1。
5、fill( 所填元素,起始位置,結束位置 )函數
函數做用:用指定的值,填充到數組。
let arr=[1,3,5,6,7]
arr.fill(10,0,1)
console.log(arr) // [10,3,5,6,7]
能夠看出會覆蓋掉原數組的值
6、entries( )函數
函數做用:對數組的鍵值對進行遍歷,返回一個遍歷器,能夠用for..of對其進行遍歷。
for(let[key,value] of ['aa','bb'].entries()){
console.log(key,value); // 0 'aa' 1 'bb'
}
7、keys( )函數
對數組的索引鍵進行遍歷,返回一個遍歷器。
for(let index of ['aa','bb'].keys()){
console.log(index); // 0 1
}
8、values( )函數
做用:對數組的元素進行遍歷,返回一個遍歷器。
for(let value of ['aa','bb'].values()){
console.log(value); // aa bb
}