今天来学习一下数组的遍历、删除等知识点:

   1 数组的遍历

数组元素的遍历可以使用for循环,采用关键字for...in

var aCity =  new Array("北京","上海","广州");     //声明一个数组

for(var i in aCity){                                         //遍历数组中的每一项

alert(aCity[i]);

}

二维数组的遍历:

var aCity = new Array();

aCity[0] = new Array();   //二维数组赋值

aCity[0][0] = "常州";

aCity[0][1] = "苏州";

aCity[1] = new Array();

aCity[1][0] = "泰州";

aCity[1][1] = "镇江";

aCity[2] = new Array();

aCity[2][0] = "徐州";

aCity[2][1] = "连云港";

for(var i in  aCity){         //遍历二维数组

for(var j in aCity[i])

alert(aCity[i][j]);

}

   2 删除元素

删除元素可以使用函数pop()和shift(),pop是从数组尾部开始删除、shift是从数组头部开始删除;

var aCity = new Array("东台","南京","上海");   
      aCity.pop();                //删除“上海”
      aCity.shift();              //删除“东台”

05-04 01:08