Jsの练习-数组常用方法 -forEach()

时间:2023-03-09 04:04:38
Jsの练习-数组常用方法 -forEach()

forEach()  : 对数组进行遍历循环,对数组中的每一项运行给定函数。

格式: arr.forEach(function(value,index){})

<!DOCTYPE html>
<html lang="en"> <head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head> <body>
<script>
var arr = [, , , , , ,'哈哈'];
arr.forEach(function (value, index) {
console.log(index + ':' + value);
})
</script> </body> </html>

Jsの练习-数组常用方法 -forEach()