JSON数组,JSON对象,数组---(建议:不要用for in遍历数组)

本文详细介绍了如何在JavaScript中遍历JSON数组,包括使用for...in和传统的for循环的方法,并对比了不同遍历方式的优劣,强调了避免使用for...in遍历数组的重要性。

对json数组的遍历:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<script type="text/javascript">
			
			
			//josn数组
			var jsonArray= [{"name":"zhang", "password":"123"},{"name":"yang", "password":"456"}];
			
			//alert(jsonArray[0].name);//zhang
			
			
			//对json数组的遍历
			for(var n in jsonArray){ //遍历json数组时,n为索引:0,1
				alert(jsonArray[n].name + " " + jsonArray[n].password);  //zhang 123  yang 456  
			}
			// 或者
			for(var i = 0; i < jsonArray.length; i++){
				alert(jsonArray[i].name + " " + jsonArray[i].password);
			}
			
			
			
		</script>
	</head>
	<body>
	</body>
</html>

https://blog.youkuaiyun.com/weixin_42103983/article/details/106743928?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.channel_param

https://blog.youkuaiyun.com/gao_sl/article/details/81699063?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param

https://blog.youkuaiyun.com/luckfairyluckbaby/article/details/101437679

各种遍历的对比:

https://www.cnblogs.com/wangxiayun/p/10194806.html

建议:不要用for in遍历数组  https://blog.youkuaiyun.com/ZHOU_VIP/article/details/108755616

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZHOU_VIP

您的鼓励将是我创作最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值