<!DOCTYPE HTML>
<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>流程控制语句</title>
<script type="text/javascript">
//第一步把之前的数据写成一个数组的形式,定义变量为 infos
var infos=[ ['小A','女',21,'大一'],
['小B','男',23,'大三'],
['小C','男',24,'大四'],
['小D','女',21,'大一'],
['小E','女',22,'大四'],
['小F','男',21,'大一'],
['小G','女',22,'大二'],
['小H','女',20,'大三'],
['小I','女',20,'大一'],
['小J','男',20,'大三']
];
/* 建立二维数组的其中一种方法:array=[[],[],````] */
document.write(infos.length+"<br>");
//第一次筛选,找出都是大一的信息
var dayi=new Array();
var i,j;
document.write("these are infos of the primary students:"+"<br>");
for(i=0;i<10;i++)
{
for(j=0;j<4;j++)
{
if(infos[i][j]=="大一")//判断哪个学生的年级是大一
{
document.write("which 'i' is come from '大一': "+ i +"<br>");
document.write(infos[i]+"<br>");
dayi[i]=infos[i];
document.write("now put these infos into a new array one by one"+"<br>");
document.write("new array:"+dayi[i]+"<br>");
document.write("------------------------------------------------------"+"<br>");
}
}
}
document.write("this is the length of the new array:"+dayi.length+"<br>");//这里是列出大一学生数组的长度
//第二次筛选,找出都是女生的信息
document.write("then these infos are women:"+"<br>");
var a,b=0;
for(a=0;a<dayi.length;a++)
{
for(b=0;b<4;b++)
{
if(dayi[a][b]=="女")
{document.write(dayi[a]+"<br>");}
}
}
</script>
</head>
<body>
</body>
</html>
问题汇总:
1.JS不允许多维数组,只有二维数组,但我用array[i].length报错
2.运行报错:
text9.html:48 Uncaught TypeError: Cannot read property ‘0’ of
undefined
at text9.html:48 text9.html:48 Uncaught TypeError: Cannot read property ‘0’ of undefined
at text9.html:48
报错原因我推测为第33行语句,只赋值了infos[i][j]"大一"的数组,其他为undefined,所以程序执行到第48行if(dayi[a][b]“女”)时识别不了
而且根本也不需要赋值到另外一个数组上再找女生,待优化