JS 数组
数组
1.空数组
var attr = new Array[]; var attr =[];
2.有值的
var attr = new Array[1,2,5,6]; var attr = [1,2,4,5];
3..数组点的数值添加方式
(1)通过索引赋值来添加 数组的索引从0开始
var attr = [1,2,4,5];
0: 1
1: 2
2: 4
3: 5
length: 4
attr[2]=3;
console.log(attr); //会把原来索引是2的4,覆盖成3
0: 1
1: 2
2: 3
3: 5
length: 4
(2)通过循环来赋值
var attr = []; for(var i = 0;i<5;i++){ attr[i]=i; } console.log(attr); //先顶一个一个空数组,循环 设i<5,循环5次。把每次i循环的数赋值给数组,打印输出数组
(3).attr.push(); push:推
/var attr = [1,2,3]; attr.push(4); attr.push(5);*/ //console.log(attr);
4.对数组取值
var attr =["苹果","香蕉","梨","椰子"]; //通过具体的索引 //console.log(attr[1]); //输出香蕉 //通过循环 //设置变量i,i<数组的索引长度,0<3,走4次,把数组所有的值都取出来 /*for(var i = 0;i<attr.length;i++){ document.write(attr[i]); }