js数组遍历

Posted 乱了夏天蓝了海

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js数组遍历相关的知识,希望对你有一定的参考价值。

  /*遍历数组方法一:普通的for循环*/
    for (var i = 0; i < arr.length; i++) {
        console.log("数组的索引:",i,"对应的值为:",arr[i]);
    }

    //数组遍历方法二:使用for...in循环
    for(i in arr){
        if(i == arr.length - 1){
            document.write(arr[i]);
        }else{
            document.write(arr[i]+",");
        }
    }

    document.write("<br>======================<br>")

    //数组遍历方法三:使用forEach循环
    //forEach()循环是ECMAScript5.0中加入的,在低版本的IE中无法使用;forEach()中不能使用break和continue
    arr.forEach(function (value, index, array) {//回调函数 匿名函数  参数:索引对应的值、索引值、数组本身
        if(index == arr.length - 1){
            document.write( "value:" + value + "&index:" + index);
        }else{
            document.write( "value:" + value + "&index:" + index + "===");
        }
    });

  

以上是关于js数组遍历的主要内容,如果未能解决你的问题,请参考以下文章