es6數(shù)組

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,而undefinednull會(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
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容