JavaScript中遍历数组的方法
Posted lotus3904
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaScript中遍历数组的方法相关的知识,希望对你有一定的参考价值。
for循环
1 let arr=["A","B","C","D","E","F"]; 2 for(let i=0;i<arr.length;i++){ 3 console.log(arr[i]); 4 }
for...of
遍历出数组中的每个值
1 let arr=["A","B","C","D","E","F"]; 2 for(let item of arr){ 3 console.log(item); 4 }
//A B C D E F
for..in
遍历出数组中每个值的下标
1 let arr=["A","B","D","E","F"]; 2 for(let item in arr){ 3 console.log(item); 4 } //0 1 2 3 4 5
ES6新增for...of的用法
- 遍历出数组中每个值的键(下标) arr.keys()
1 let arr=["A","B","C","D","E","F"]; 2 for(let item of arr.keys()){ 3 console.log(item); 4 } 5 //0 1 2 3 4 5
- 遍历出数组中的每个值 arr.values()
1 let arr=["A","B","C","D","E","F"]; 2 for(let item of arr.values()){ 3 console.log(item); 4 } 5 //A B C D E F
- 遍历出数组中的每个值,以及每个值对应的下标 arr.entries()
1 let arr=["A","B","C","D","E","F"]; 2 for(let item of arr.entries()){ 3 console.log(item); 4 } 5 /** 6 [ 0, ‘A‘ ] 7 [ 1, ‘B‘ ] 8 [ 2, ‘C‘ ] 9 [ 3, ‘D‘ ] 10 [ 4, ‘E‘ ] 11 [ 5, ‘F‘ ] 12 **/
以上是关于JavaScript中遍历数组的方法的主要内容,如果未能解决你的问题,请参考以下文章