while循環遍歷數組的方法
來源:懂視網
責編:小采
時間:2020-11-27 20:12:41
while循環遍歷數組的方法
while循環遍歷數組的方法:在看jQuery源碼時發現了這段代碼, 自己試了一下, 簡單的記錄下來.var arr = [ 'a', 'b', 'c', 'd' ], i = 0;while( arr[i++] ){ console.log( arr[i] ); /* 輸出 b c d undef
導讀while循環遍歷數組的方法:在看jQuery源碼時發現了這段代碼, 自己試了一下, 簡單的記錄下來.var arr = [ 'a', 'b', 'c', 'd' ], i = 0;while( arr[i++] ){ console.log( arr[i] ); /* 輸出 b c d undef

在看jQuery源碼時發現了這段代碼, 自己試了一下, 簡單的記錄下來.
var arr = [ 'a', 'b', 'c', 'd' ],
i = 0;while( arr[i++] ){
console.log( arr[i] );
/*
輸出 b c d undefined
*/
}
代碼執行順序為:
(1)判斷while(arr[i])是否存在,若存在,執行 (2) (3)
(2) i++
(3) console.log( arr[i] )
所以, 可以寫成 console.log( arr[i-1] )
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com
while循環遍歷數組的方法
while循環遍歷數組的方法:在看jQuery源碼時發現了這段代碼, 自己試了一下, 簡單的記錄下來.var arr = [ 'a', 'b', 'c', 'd' ], i = 0;while( arr[i++] ){ console.log( arr[i] ); /* 輸出 b c d undef