$.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>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章