細(xì)說(shuō)JS數(shù)組遍歷的一些細(xì)節(jié)及實(shí)現(xiàn)
目錄
- 1. 數(shù)組空位問(wèn)題 ?
- 1.1 空位判斷 ?
- 1.2 剛列舉了數(shù)組的一些操作會(huì)對(duì)空位進(jìn)行跳過(guò),但其實(shí),它們?cè)谔幚砩弦策€是存在一些差異的
- 2. 是否修改原數(shù)組 ?
- 3. 附一下數(shù)組遍歷的幾種方法 ?
- 3.1 索引訪問(wèn) ?
- 3.2 for ... of ?
- 3.3 forEach ?
- 3.4 for ... in ?
- 3.5 map、filter、reduce ?
1. 數(shù)組空位問(wèn)題 ?
數(shù)組空位是什么,它是數(shù)組內(nèi)某個(gè)元素沒(méi)有任何值,這種現(xiàn)象稱為空位現(xiàn)象,我們?cè)谑褂肁rray()去構(gòu)造一個(gè)數(shù)組時(shí),如果只傳入一個(gè)參數(shù),數(shù)組里面的元素項(xiàng)就會(huì)出現(xiàn)空位現(xiàn)象,它其實(shí)是沒(méi)有任何元素的,下面是幾個(gè)會(huì)創(chuàng)建空位的典型例子:
let arr = Array(5) // <5 empty items> let res = arr.map(e => e + 1) // <5 empty items> let a = [,,]; // <2 empty items>
ES5 中的數(shù)組方法包括:map、filter、forEach、reduce,還有 for ... in 等語(yǔ)法,這些方法執(zhí)行的時(shí)候遇到空位,會(huì)直接跳過(guò)。
// ES5 及以前,不會(huì)處理空位 const a = Array(5); console.log(a) // [ <5 empty items> ] console.log(a.map(_ => 1)) // 無(wú)效,[ <5 empty items> ] // console.log(a.reduce((p, c)=> p+c)) // 報(bào)錯(cuò) console.log(a.filter(x => true)) // [] a.forEach(e => { console.log(e); }) // 無(wú)任何輸出 console.log(a+"") // ,,,, console.log(a.indexOf(undefined)) // -1 console.log(a.lastIndexOf(undefined)) // -1 for (let k in a) { console.log(k) // 無(wú)任何輸出 }
ES5中將空位視為undefined的有:join(),toString()
[,"a","b",undefined,null].join("@") // "@a@b@@" [,"a","b",undefined,null].toString() // ",a,b,,"
ES6,一些操作會(huì)將數(shù)組空位視為undefined來(lái)處理。比如下面的一些操作
console.log([...a]) // [ undefined x 5 ] console.log(Array.from(a)) // [ undefined x 5 ] console.log(a.includes(undefined)) // true console.log(a.find(x => x === undefined)) // undefined console.log(a.findIndex(x => x === undefined)) // 0 // for of 循環(huán)取值 for (let k of a) { console.log(k) // 輸出5個(gè)undefined }
1.1 空位判斷 ?
我們可以使用in運(yùn)算符來(lái)判斷數(shù)組某個(gè)位置是否存在空位
let arr = Array(3) console.log(0 in arr) // false
如上述所示,使用in運(yùn)算符,判斷索引為0的位置是否為空位,遇到數(shù)組空位會(huì)返回false
這里需要注意的是所謂的空位是沒(méi)有任何值的,undefined、null它們都不屬于空位,下面來(lái)判斷一下。
let arr = [undefined, null]; console.log(0 in arr) // true console.log(1 in arr) // true
驗(yàn)證了undefined、null所在的位置它不是空位
1.2 剛列舉了數(shù)組的一些操作會(huì)對(duì)空位進(jìn)行跳過(guò),但其實(shí),它們?cè)谔幚砩弦策€是存在一些差異的
比如:forEach、filter、some與every等在遇到空位時(shí),會(huì)直接跳過(guò)它,不會(huì)保留它的值, 而map則會(huì)保留空位
我們來(lái)看下例子:
[,1,2].forEach((x,i) => { console.log(i); }) // 這里只會(huì)輸出1 2 [,1,2].filter((x,i) => { return x > 0; }) // 這里也只會(huì)輸出[1, 2] [,1,2].map((x) => { return x > 0; }) // 這里會(huì)輸出[空, true, true],它保留了空位
其余的可以自行測(cè)試看看結(jié)果
2. 是否修改原數(shù)組 ?
請(qǐng)看以下代碼,遍歷過(guò)程中,試圖修改遍歷的每一項(xiàng)。
const arr = [1, 2, 3] let res = arr.filter(item => { item++ return item >= 3 }) console.log(arr) // [ 1, 2, 3 ] console.log(res) // [ 2, 3 ]
filter 里的 item 自增了,所以最后有兩項(xiàng)符合過(guò)濾規(guī)則,但是原數(shù)組并沒(méi)有變。這說(shuō)明這里的 item 只是原數(shù)組項(xiàng)的一個(gè)值拷貝,數(shù)組遍歷是按值傳遞的。
再看一段代碼,這次我們遍歷引用數(shù)據(jù)類型的數(shù)組。
const brr = [ {count: 1}, {count: 1}, {count: 1} ] const res2 = brr.map(item => { item.count++ return item.count }) console.log(brr) // [ { count: 2 }, { count: 2 }, { count: 2 } ] console.log(res2) // [ 2, 2, 2 ]
這里的修改改變了原數(shù)組,那之前說(shuō)的按值傳遞有問(wèn)題?其實(shí)還沒(méi)問(wèn)題的,數(shù)組函數(shù)方法說(shuō)到底也是一個(gè)函數(shù),JS 的函數(shù)總是按值傳遞的:
基本數(shù)據(jù)類型(包含變量、函數(shù)參數(shù)等)存儲(chǔ)在棧(stack)中,傳遞參數(shù)會(huì)復(fù)制一份值
引用數(shù)據(jù)類型的引用(指針)存儲(chǔ)在棧中,指向的值存儲(chǔ)在堆(heap)中,傳遞參數(shù)會(huì)復(fù)制一份對(duì)象的引用地址,復(fù)制的引用地址和原引用地址指向堆中同一個(gè)對(duì)象(因此修改參數(shù),也修改了原對(duì)象)
3. 附一下數(shù)組遍歷的幾種方法 ?
3.1 索引訪問(wèn) ?
const arr = [1, 2, 3] for (let i = 0; i < arr.length; i++) { console.log(arr[i]) }
3.2 for ... of ?
const arr = [1, 2, 3] for (let e of arr) { console.log(e) }
3.3 forEach ?
注意:return 和 break 無(wú)法中斷遍歷。
- return 可以跳過(guò)本次遍歷,但剩余元素仍然會(huì)繼續(xù)遍歷下去。
- break 只能中斷 for 和 while 循環(huán),forEach 函數(shù)中使用會(huì)報(bào)錯(cuò)
const arr = [1, 2, 3] arr.forEach((e, i, a) => { console.log(e) })
3.4 for ... in ?
`for ... in` 是用來(lái)遍歷對(duì)象(plain object)的,也可以用來(lái)遍歷數(shù)組,但不建議。
const arr = [1, 2, 3] for (let i in arr) { console.log(arr[i]) }
3.5 map、filter、reduce ?
這三類都是為了對(duì)數(shù)組進(jìn)行一個(gè)操作,然后得到目標(biāo)結(jié)果的數(shù)組方法,從功能和語(yǔ)義上來(lái)講,和 forEach 有區(qū)別的,不建議混用,尤其是 map 和 forEach。但是也能對(duì)數(shù)組進(jìn)行遍歷,詳細(xì)可參考MDN。
到此這篇關(guān)于細(xì)說(shuō)JS數(shù)組遍歷的一些細(xì)節(jié)及實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)JS數(shù)組遍歷內(nèi)容請(qǐng)搜索以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持!
相關(guān)文章:
1. Python如何批量獲取文件夾的大小并保存2. IntelliJ IDEA2020.2.2創(chuàng)建Servlet方法及404問(wèn)題3. Python的Tqdm模塊實(shí)現(xiàn)進(jìn)度條配置4. Python 多線程之threading 模塊的使用5. Python中Selenium模塊的使用詳解6. Python基于smtplib模塊發(fā)送郵件代碼實(shí)例7. CSS代碼檢查工具stylelint的使用方法詳解8. 在vue中封裝方法以及多處引用該方法詳解9. 淺談python多線程和多線程變量共享問(wèn)題介紹10. CSS3中Transition屬性詳解以及示例分享
