慕婉清8197094
2018-05-29 16:02
//第一步把之前的数据写成一个数组的形式,定义变量为 infos
var infos=[["小A","女",21,"大一"],["小B","男",23,"大三"],["小C","男",24,"大四"],["小D","女",21,"大一"],["小E","女",22,"大四"],["小F","男",21,"大一"],["小G","女",22,"大二"],["小H","女",20,"大三"],["小I","女",20,"大一"],["小J","男",20,"大三"]];
//第一次筛选,找出都是大一的信息
for(var i=0;i<infos.length;i++;){
infos[i]=new Array();
for(var j=0;j<infos[i].length;j++;){
if(infos[i][3]=="大一" && infos[i][1]=="女"){
document.write(infos[i]+"<br />");
}
else
{
continue;
}
}
}
//第二次筛选,找出都是女生的信息
</script>
for(var i=0;i<infos.length;i++){ if(infos[i][3]=="大一" && infos[i][1]=="女"){ console.log(infos[i]+"<br />"); } }
首先不需要嵌套循环哦,两个筛选条件可以用&&连接那就不需要2次循环去判断,其次你内部的循环
infos[i]=new Array();
这行代码重新初始化了infos[i],那就不会有数据
<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,"大三"]];
//第一次筛选,找出都是大一的信息
document.write("都是大一的信息:"+"<br/>");
for( var i=0;i<infos.length;i++){
if(infos[i][3]=="大一"){
document.write(infos[i]+"<br />");
}
}
document.write("都是女生的信息:"+"<br/>");
for( var i=0;i<infos.length;i++){
if(infos[i][1]=="女"){
document.write(infos[i]+"<br />");
}
}
</script>
你在for里面又定义了一个infos数组和for外面的数组有冲突,名字一样。
<!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,'大三']];
//第一次筛选,找出都是大一的信息
for(var i=0;i<infos.length;i++){
if(infos[i][3]=="大一" && infos[i][1]=="女"){
document.write(infos[i][0]+"<br>")
}
}
//第二次筛选,找出都是女生的信息
</script>
</head>
<body>
</body>
</html>
JavaScript进阶篇
468194 学习 · 21891 问题
相似问题