什么是数组的遍历
数组的遍历就是依次取出数组中存储的所有数据, 我们就称之为数组的遍历。
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Demo</title>
<script type="text/javascript">
let arr = ["a", "b", "c"];
console.log(arr[0]);
console.log(arr[1]);
console.log(arr[2]);
</script>
</head>
<body>
</body>
</html>
紧接着再往数组当中添加两个元素,可以调用一个 length
方法获取数组的元素长度
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Demo</title>
<script type="text/javascript">
let arr = ["a", "b", "c"];
console.log(arr[0]);
console.log(arr[1]);
console.log(arr[2]);
arr[3] = "d";
arr[4] = "f";
console.log(arr.length);
</script>
</head>
<body>
</body>
</html>
有了长度之后就可以利用循环来进行数组的遍历了
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Demo</title>
<script type="text/javascript">
let arr = ["a", "b", "c"];
console.log(arr[0]);
console.log(arr[1]);
console.log(arr[2]);
arr[3] = "d";
arr[4] = "f";
for (let i = 0; i < arr.length; i++) {
console.log(arr[i]);
}
</script>
</head>
<body>
</body>
</html>