慕沐9023737
2016-10-18 14:57
var infos=[
["小A","女",21,"大一"],
["小B","男",23,"大三"],
["小C","男",24,"大四"],
["小D","女",21,"大一"],
["小E","女",22,"大四"],
["小F","男",21,"大一"],
["小G","女",22,"大二"],
["小H","女",20,"大三"],
["小I","女",20,"大一"],
["小J","男",20,"大三"]
];
//第一次筛选,找出都是大一的信息
var arr=[];
n = 0;
for (i=0;i<infos.length;i++){
if (infos[i][3]=="大一"){
arr[n]=infos[i]
}
document.write("大一学生:"+arr[n]);
n=n+1;
}
document.write("大一人数: "+arr.length+"<br/>");
//第二次筛选,找出都是女生的信息
for (i=0;i<arr.length;i++){
if (arr[i][1]=="女"){
docuemnt.write("大一女生:"+arr[i][0]);
}
}
两个if可以直接在for循环里判断完成,你的代码有些冗余了。我复制你的代码, if (infos[i][3]=="大一")这句话的括号是中文的,你看看。。。
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title> 速度加快立法角度思考了 </title>
<script type="text/javascript">
var infos=[
["小A","女",21,"大一"],
["小B","男",23,"大三"],
["小C","男",24,"大四"],
["小D","女",21,"大一"],
["小E","女",22,"大四"],
["小F","男",21,"大一"],
["小G","女",22,"大二"],
["小H","女",20,"大三"],
["小I","女",20,"大一"],
["小J","男",20,"大三"]
];
var arr=[];
var n = 0;
for (i=0;i<infos.length;i++){
if (infos[i][3]=="大一"){ //你这()有错
arr[n]=infos[i];
document.write("大一学生:"+arr[n]); //你这逻辑错误
n=n+1;
}
}
document.write("大一人数: "+arr.length+"<br/>");
//第二次筛选,找出都是女生的信息
for (i=0;i<arr.length;i++){
if (arr[i][1]=="女"){
document.write("大一女生:"+arr[i][0]); //你这拼写错误
}
}
</script>
</head>
<body>
</body>
</html>
你的问题可以复制到电脑上的软件里运行下,好找出问题,代码中有几个问题:
if 后面是中文的括号
最后一个 document 单词写错了
document.write("大一学生:"+arr[n]);
n=n+1;
这两行改写至 if 里面
你可以试下。
JavaScript进阶篇
468194 学习 · 21891 问题
相似问题