今天注意学习了数组,上午做了一个案例没鼓捣了半天才弄明白。今天数组的内容整理的不多,明天还需要深入了解,这块内容很重要,还有函数。
数组
使用单独的变量名,储存多个值。
用new方法来创建一个数组(不是很常用);
var ary1=new Array(); //用于在单个变量中存储多个值
var my=new Array(3) my[0]="sa" my[1]="2" my[2]=3
var my=new Array(1,2,3) alert(my)
直接创建一个数组, 通过索引值给数组赋值(经常用)
script type="text/javascript"> var ary1=[]; ary1[0]=12; ary1[1]=23; ary1[2]="中国人"; ary1[3]="美国人"; alert(ary1) </script>
数组的初始化
var ary1=[12,23,45,"中国人","34"]; alert(ary1)
数组的遍历(把每个数组里的值读一遍)
var ary1=[12,23,45,"中国人","34"]; for(i=0; i<5;i++){ alert(ary1[i]); };
数组数据的个数 length属性
通过数组名.length获取数组长度(元素个数)
var ary1=[12,23,45,"中国人","34",5,5,8,8,6,2]; for(i=0; i<ary1.length;i++){ alert(ary1[i]); }; </script>
数组合并(concat)
var ary1=[12,23,45,"中国人","34",5,5,8,8,6,2]; var ary2=["试试","试试","试试","试试","试试","试试"] var ary3=["1","1","1","1","1"] var ary4=ary2.concat(ary1,ary3); alert(ary4)
Join方法 返回一个字符串
var ary1=[12,23,45,"中国人","34",5,5,8,8,6,2]; var ary2=ary1.join("-") alert(ary2);
案例
var ary1=[]; for(var i=1; i<=10; i++){ ary1[i]=i; } alert(ary1) </script>
找出数组中偶数
var ary1=[1,2,3,4,5,6,7,8,9]; for(var i=0; i<=ary1.length;i++){ if(ary1[i]%2==0){ console.log(ary1[i]) }; };
时间: 2024-10-10 07:35:51