您好,登錄后才能下訂單哦!
這篇文章主要介紹了JavaScript遍歷數組的方法代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
for循環
let arr=["A","B","C","D","E","F"]; for(let i=0;i<arr.length;i++){ console.log(arr[i]); }
for...of
遍歷出數組中的每個值
let arr=["A","B","C","D","E","F"]; for(let item of arr){ console.log(item); } //A B C D E F
for..in
遍歷出數組中每個值的下標
let arr=["A","B","D","E","F"]; for(let item in arr){ console.log(item); } //0 1 2 3 4 5
ES6新增for...of的用法
遍歷出數組中每個值的鍵(下標) arr.keys()
let arr=["A","B","C","D","E","F"]; for(let item of arr.keys()){ console.log(item); } //0 1 2 3 4 5
遍歷出數組中的每個值 arr.values()
let arr=["A","B","C","D","E","F"]; for(let item of arr.values()){ console.log(item); } //A B C D E F
遍歷出數組中的每個值,以及每個值對應的下標 arr.entries()
let arr=["A","B","C","D","E","F"]; for(let item of arr.entries()){ console.log(item); } /** [ 0, 'A' ] [ 1, 'B' ] [ 2, 'C' ] [ 3, 'D' ] [ 4, 'E' ] [ 5, 'F' ] **/
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。