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>