js数组遍历的几种方式

        var team = ['zhangsan','lisi','wagnwu'];
        //遍历数组方式1 - for循环
        for(var i=0;i<team.length;i++){
            console.log(team[i]);
        };

        //遍历数组方式2 - 索引遍历
        for(var i in team){
            console.log(team[i]);
        };

        //遍历数组方式3 - 数组对象.forEach方法
        team.forEach(function(v,k){
            console.log(v,k);
        });

        team.forEach( function(v) {
            console.log(v);
        });

        //遍历数组方式4 - for of 值遍历(ES6)
        for(var v of team){
            console.log(v);
        }

 

posted @ 2021-02-21 20:42  华北业余选手  阅读(319)  评论(0)    收藏  举报