E5中遍历数组的方法

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	
	<script type="text/javascript">
		var arr = [11, 22, 33, 44, 55, 66];
		// 遍历数组,原先的做法:使用for循环
		// for(var i = 0; i < arr.length; i++) {
		// 	console.log(arr[i]);
		// }

		// ES5中提供了:
		// 1 forEach
		// 2 map
		// 
		// 作用:都是来遍历数组的。
		// map :不仅能遍历数组而且是能返回一个新的数组

		// forEach 和 map 的参数:一个回调函数
		// 回调函数的两个参数:
		// 第一个参数表示:数组的当前值
		// 第二个参数表示:当前的索引号
		// arr.forEach(function(value, index) {})

		/*arr.forEach(function(value, index) {
			// console.log(value);
			console.log("索引号为:" + index + ", 值为:" + value);

			// this 指向了 window
		});*/

		var newArr = arr.map(function(value, index) {
			// console.log("索引号为:" + index + ", 值为:" + value);
			// return value * 2;
			// return value * index;
			return "a";
		});

		// newArr.forEach(function() {});
		console.log(newArr);
	</script>
</body>
</html>

 

posted @ 2016-09-22 21:38  萧诺  阅读(207)  评论(0编辑  收藏  举报