$.each的使用

$.each的使用

<script>
			// 1.遍历一维数组
			var arr=["aa","bb","cc","dd","ee"];
			$.each(arr,function(i,val){
				console.log(i+'...'+val)
			})
			// 2.遍历二维数组
			var arr1=[["aa","bb"],["cc","dd"],["ee","ff"],["gg","hh"]];
			$.each(arr1,function(i,item){
				console.log(i+' '+item);
				// 此时可以对输出的一维数组进行遍历
				$.each(item,function(i,val){
					console.log(i+' '+val);
				})
			})
			
			// 3.处理json.
			var json1={key1:'a',key2:'b',key3:'c'};
			$.each(json1,function(key,value){
				console.log(key+' '+value);
			})
			// 4.当二维数组中有json对象时
			var arr3=[{name:"star",age:"16"},{name:"star1",age:"17"},{name:"star2",age:"18"}];
			$.each(arr3,function(key,val){
				console.log(key+' '+val);
				console.log(val.name);
				console.log(val["name"]);
				$.each(val,function(key,val2){
					console.log(key+' '+val2);
				})
			})
		</script>
原文地址:https://www.cnblogs.com/lxystar/p/10469236.html