码迷,mamicode.com
首页 > 编程语言 > 详细

JavaScript中遍历数组的方法

时间:2020-01-12 11:28:14      阅读:104      评论:0      收藏:0      [点我收藏+]

标签:ons   span   let   pre   nbsp   key   rip   用法   trie   

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的用法

  1. 遍历出数组中每个值的键(下标)  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

     

  2. 遍历出数组中的每个值  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

     

  3. 遍历出数组中的每个值,以及每个值对应的下标  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中遍历数组的方法

标签:ons   span   let   pre   nbsp   key   rip   用法   trie   

原文地址:https://www.cnblogs.com/Lotus3904/p/12182023.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!