JavaScript 中for循环总结

在for循环中,设置变量区域为父级区域,循环体内为子级区域

  1. 常规写法:这种写法比较常用,index为数字(number),操作下标,与for…of相比,缺点,无法识别大于0xFFFF的码点
 			var arr=[1,2,3,4,5];
			for(let index=0;index<arr.length;index++){
				console.log(index+","+arr[index]);
			} //0,1//1,2//2,3//3,4//4,5

			// 0x20BB7:"𠮷"
			let s=String.fromCodePoint(0x20BB7);						
			for (let i = 0; i < s.length; i++) {
			  console.log(s[i]);
			}//打印出两个未知字符//�//�

			function f(){
				for(let index=0;index<arr.length;index++){
					if (index === 3) {
						return "找到了位置3,终止循环";
					}
					console.log(index+","+arr[index]);
				}				
			}
			console.log(f());//0,1//1,2//2,3//找到了位置3,终止循环

String.fromCharCode(code)方法可以实现把码点转成字符打印,但是无法识别大于0xFFFF的码点,而ES6出现了fromCodePoint(code1,code2,code3,…),并且该方法可以同时传入多个参数,会自动对参数进行字符串拼接。

  1. forEach:ES5出现的方法,index即为元素的值,会自适应值的类型,与原始for循环相比,缺点,无返回值,并且无法跳出循环,break命令或return命令都不能奏效。
			function f() {
				arr.forEach(function(index) {
					console.log(typeof(index));
					if (index === 3) {
						return "找到了位置3,终止循环";
					}
					console.log(index);
				});
			}
			console.log(f());//1//2//4//5//undefined

3.for…in:ES5出现的方法,适合操作对象,该index为字符串(string),操作下标或为key

 			for(let index in arr){
				console.log(typeof(index));//string
				console.log(arr[index]);//1,2,3,4,5弱类型语言的兼容太强,最好不要这样做
			} 
			let obj={a:1,b:2,c:3}
			for (let index in obj){
				console.log(index);//a,b,c
				console.log(obj[index]);//1,2,3//读取对象值的方式
			}
			
 			let cars = new Array()
			cars[0] = 'Saab'
			cars[1] = 'Volvo'
			cars[3] = 'BMW'
			for (let index in cars) {
				console.log(typeof(index)); //string
				console.log(index + "," + cars[index]);
			} //0,Saab//1,Volvo//3,BMW

			for (let index = 0; index < cars.length; index++) {
				console.log(typeof(index)); //number
				console.log(index + "," + cars[index]);
			} //0,Saab//1,Volvo//2,undefined//3,BMW 

4.for…of,ES6写法,支持数组,类数组对象(如:NodeList对象),Map,Set,字符串等,index即为元素的值,会自适应值的类型,但解析对象必须拥有遍历器(Iterator)接口。常用来解析字符

			for(let index of arr){
				console.log(typeof(index));//number
				console.log(index);//1,2,3,4,5
			} 
			
			let s=String.fromCodePoint(0x20BB7);
			for (let index of s){
				console.log(typeof(index));//string
				console.log(s);//"𠮷"
			}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JavaScript,使用for循环遍历数组是一种常见的方法。可以通过循环变量逐一访问数组元素。下面是一个示例代码: const arr = [1, 2, 3, 4, 5]; for (let i = 0; i < arr.length; i++) { console.log(arr[i]); } 这段代码,我们定义了一个数组arr,然后使用for循环遍历数组的每个元素,并通过console.log打印出来。循环变量i从0开始,每次循环递增1,直到i小于数组长度为止。在循环体内,通过arr[i]可以获取到当前循环的数组元素。这样就可以依次访问数组的每个元素了。[1] 除了上述常规的for循环,还可以使用其他的for循环形式来遍历数组。例如,可以使用forEach方法来遍历数组,它提供了更简洁的语法: const arr = [1, 2, 3, 4, 5]; arr.forEach(function(element) { console.log(element); }); 这段代码,我们调用了数组的forEach方法,传入一个回调函数作为参数。回调函数的element参数表示当前遍历的数组元素,通过console.log(element)可以打印出每个元素。forEach方法会自动遍历数组的每个元素,并依次执行回调函数。[2] 另外,还可以使用for...of循环来遍历数组,它提供了更简洁的语法: const arr = [1, 2, 3, 4, 5]; for (let element of arr) { console.log(element); } 这段代码,我们使用for...of循环遍历数组arr,每次循环将当前元素赋值给变量element,然后通过console.log(element)打印出来。for...of循环会自动遍历数组的每个元素,并依次执行循环体。[3] 总结起来,JavaScript可以使用for循环、forEach方法或for...of循环来遍历数组,根据实际需求选择合适的方式即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值