js循环遍历数组

一维数组的遍历

<script>
  //循环遍历数组
  var animals = ["cat",'dog','human','whale','seal'];
  var animalString = "";
  for(var i = 0;i<animals.length;i++){
  animalString += animals[i] + " ";
  }
  alert(animalString); //输出数组里的每个项
</script>

遍历二维数组

<script>
  var arr=[[0,0,0,0,0,0],[0,0,1,0,0,0],[0,2,0,3,0,0],[0,0,0,0,0,0]];
  for(var i=0;i<arr.length;i++){
  //遍历每一个具体的值
  for(var j=0;j<arr[i].length;j++){
  document.writeln(arr[i][j]+" ");
  }
  document.writeln("<br/>");
  }
</script>
上一篇:for循环遍历数组(数组1)


下一篇:JavaScript基础精华03(String对象,Array对象,循环遍历数组,JS中的Dictionary,Array的简化声明)