当前位置:首页 >> 网络编程

JavaScript遍历数组的方法代码实例

这篇文章主要介绍了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' ]
**/

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。