js 遍历数组

<!DOCTYPE html>  
<html>  
<head>  
    <meta charset="utf-8"/>  
    <title>数组的遍历方式</title>  
    <script type="text/javascript">  
        var arr = [11,22,33,55];  
        //普通的循环遍历方式  
        function first(){  
                  
            for(var i= 0;i<arr.length;i++){  
                console.log("第一种遍历方式\t"+arr[i]);  
            }  
            console.log("111111111111111111111111111111");  
  
  
        }  
        //2、for ..in 用于遍历对象的,遍历数组不要用  
        function second(){  
            // for in 遍历需要两个形参 ,index表示数组的下标(可以自定义),arr表示要遍历的数组  
            for(var index in arr){  
                console.log("第二种遍历方式\t"+arr[index]);  
  
            }  
            console.log("222222222222222222222222222222");  
        }  
  
        //3,for each很鸡肋的遍历方式  
        function third(){  
            //第一个参数为数组的元素,第二个元素为数组的下标  
            arr.forEach(function(ele,index){  
                console.log("第三种遍历方式\t"+arr[index]+"-----"+ele);  
            });  
            console.log("333333333333333333333333333333");  
  
        }  
     //4,map

      [1,2,3,4].map(function(item){
          alert(item)
        })
 
  

 


//5,for-of遍历方式 function forth(){ //第一个变量ele代表数组的元素(可以自定义) arr为数组(数据源) for(var ele of arr){ console.log("第四种遍历方式\t"+ele); } console.log("444444444444444444444444444444"); } </script> </head> <body> <input type="button" value="第一种遍历方式" name="aa" onclick="first();"/><br/> <input type="button" value="第二种遍历方式" name="aa" onclick="second();"/><br/> <input type="button" value="第三种遍历方式" name="aa" onclick="third();"/><br/> <input type="button" value="第四种遍历方式" name="aa" onclick="forth();"/><br/> </body> </html>

 

转载于:https://www.cnblogs.com/dongtianqi/p/7813565.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值