Array.from()
將類數(shù)組轉(zhuǎn)成真正的組轉(zhuǎn)
let ArrayLike = {
'0': 'a',
'1': 'b',
'2': 'c',
length: 3
}
console.log(Array.from(ArrayLike,x=>x+'xx'))
// 等價(jià)于
console.log(Array.from(ArrayLike).map(x=>x+'xx'))
Array.of()
將一維數(shù)轉(zhuǎn)成數(shù)組
console.log(Array.of(1,2,3)) //[1,2,3]
console.log(Array.of(1,2,3).length) //3
find
找到第一個(gè)符合條件的數(shù)組
console.log([1,2,3,4,5].find((value,index,arr)=>value<1)) //undefined
console.log([1,2,3,4,5].find((value,index,arr)=>value<3)) //1
findIndex
和find類似,他是返回第一個(gè)符合條件的下標(biāo)
console.log([1,2,3,4,5].findIndex((value,index,arr)=>value<1)) //-1
console.log([1,2,3,4,5].findIndex((value,index,arr)=>value<3)) //0
for...of
keys() values() entries()
for(let i of ['a','b'].values()){console.log(i)} //a,b
for(let i of ['a','b'].keys()){console.log(i)} //0,1
for(let [index,value] of ['a','b'].entries()){console.log(index,value)} //0 a,1 b
includes
返回一個(gè)布爾值,判斷數(shù)據(jù)是否有某數(shù)據(jù),和字符串的includes類似
includes 的第二個(gè)參數(shù)表示搜索的起始位置
console.log('abc'.includes('a')) //true
console.log(['a','b'].includes('a')) //true
console.log(['a','b'].includes('a',0)) //true
console.log(['a','b'].includes('a',1)) //false
數(shù)組空值
-
for...of會(huì)遍歷數(shù)組空位,forEach(),filter(),every(),map()和some()會(huì)跳過數(shù)據(jù)空位
for(let i of [,,,]){
console.log(1)
}//1,1,1
-
Array.from(),擴(kuò)展運(yùn)算符(...)都會(huì)把數(shù)組空位轉(zhuǎn)成undefined
console.log([...[,,,]]) //[undefined,undefined,undefined]
console.log(Array.from([,,,])) //[undefined,undefined,undefined]
-
join,toString,會(huì)把數(shù)據(jù)空位視為undefined,而undefined和null會(huì)處理成空串
console.log([,,,1].join("*"))//***1
console.log([,,,2].toString())//,,,2
-
entries()、keys()、values()、find()和findIndex()會(huì)將空位處理成undefined。
console.log([...[,'a'].keys()]) //[0, 1]
console.log([...[,'a'].values()]) //[undefined, "a"]
console.log([...[,'a'].entries()]) //[[0, 1],[undefined, "a"]]
[,'a'].find(x => true) // undefined
[,'a'].findIndex(x => true) // 0