<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
var names=['李大','李二','李三','李四','李五']
// 方法一
for(var i=0;i<names.length;i++){
console.log(i,names[i]);
}
// 方法二
for(const key in names){
console.log(key,names[key]);
}
// 方法三
for(const value of names){
console.log(value);
}
// 方法四
names.forEach((value,index,array)=>{
console.log(value,index);
})
</script>
</body>
</html>
本文通过一个简单的HTML页面示例,介绍了使用JavaScript遍历数组的四种常见方法:传统for循环、for...in循环、for...of循环以及Array.prototype.forEach方法。每种方法都有其适用场景和特点。

722

被折叠的 条评论
为什么被折叠?



