<!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,很雞肋的遍歷方式 function third(){ //第一個參數為數組的元素,第二個元素為數組的下標 arr.forEach(function(ele,index){ console.log("第三種遍歷方式\t"+arr[index]+"-----"+ele); }); console.log("333333333333333333333333333333"); } //4,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>