$.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>
posted @ 2019-03-04 10:29  lxystar  阅读(170)  评论(0编辑  收藏  举报