<!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,'大三']];
var freshMan[][];
var a = 0;
//第一次筛选,找出都是大一的信息
for(var i = 0;i < infos.length;i++){
if(infos[i][3] == "大一"){
document.write(infos[i][0]);
freshMan[a][0] = infos[i][0];
freshMan[a][1] = infos[i][1];
a = a + 1;
}
}
//输出筛选后的数组
for(var n = 0;n < freshMan.length;n++){
for(var p = 0;p < freshMan[0].length;p++){
document.write(freshMan[n][p]);
}
}
//第二次筛选,找出都是女生的信息
for(var s = 0;s < freshMan.length;s++){
if(freshMan[s] == "女"){
document.write(freshMan[s][0]);
}
}
</script>
</head>
<body>
</body>
</html>
点提交后无打印输出。
if(infos[i][3] == "大一"){
document.write(infos[i][0]+"<br>");
freshMan[a] = new Array();
freshMan[a][0] = infos[i][0];
freshMan[a][1] = infos[i][1];
a = a + 1;
}
添加上加粗的那句话就行了,你需要在赋值二维数组前 申明一维数组的某个元素也是数组
infos[][]还有freshMan[][]的 两个[]什么意思?要去掉吧;
还有var a = 0;去掉,for循环里面已经有var i = 0了,不用再定义一个a;
有些代码也重复了。这样,你参考一下我的:
<!DOCTYPE HTML> <html > <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>流程控制语句</title> <script type="text/javascript"> let infos = new Array(['小A','女',21,'大一'], ['小B','男',23,'大三'], ['小C','男',24,'大四'], ['小D','女',21,'大一'], ['小E','女',22,'大四'], ['小F','男',21,'大一'], ['小G','女',22,'大二'], ['小H','女',20,'大三'], ['小I','女',20,'大一'], ['小J','男',20,'大三']); for (let i = 0; i < infos.length; i++) { // document.write(infos[i] + "<br />"); if (infos[i][3] === "大一") { // document.write(infos[i]); if (infos[i][1] === "女") { document.write(infos[i][0] + "是大一的女生" + "<br />"); } } } </script> </head> <body> </body> </html>