weibo_
2016-07-15 10:53
<!DOCTYPE HTML>
<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>流程控制语句</title>
<script type="text/javascript">
//第一步把之前的数据写成一个数组的形式,定义变量为 infos
var infos=new Array();
infos=[
['小A','女',21,'大一'],
['小B','男',23,'大三'],
['小C','男',24,'大四'],
['小D','女',21,'大一'],
['小E','女',22,'大四'],
['小F','男',21,'大一'],
['小G','女',22,'大二'],
['小H','女',20,'大三'],
['小I','女',20,'大一'],
['小J','男',20,'大三']
]
//第一次筛选,找出都是大一的信息
var j=0;
var childinfos=new Array();
for(var i=0;i<infos.length;i++){
switch(infos[i][3]){
case "大一":
childinfos[j]=infos[i];
document.write(childinfos[j]+'<br/>');
j++;
}
}
document.write(childinfos.length+"<br/>");
document.write("<br/>");
//第二次筛选,找出都是女生的信息
for(var k=0;k<childinfos.length;k++){
if(childinfos[k][1]=="女"){
document.write(childinfos[k][0]+'<br/>');
}
}
</script>
</head>
<body>
</body>
</html>
<script type="text/javascript">
function Stu(name,sex,age,grade){
var stu = new Object()
stu.name=name;
stu.sex =sex;
stu.age = age;
stu.grade = grade;
return stu;
}
var stus = [];
stus[0] = Stu("小A","女",21,"大一");
stus[stus.length] = Stu("小B","男",23,"大三");
stus[stus.length] = Stu("小C","男",24,"大四");
stus[stus.length] = Stu("小D","女",21,"大一");
stus[stus.length] = Stu("小E","女",22,"大四");
stus[stus.length] = Stu("小F","男",21,"大一");
stus[stus.length] = Stu("小G","女",22,"大二");
stus[stus.length] = Stu("小H","女",20,"大三");
stus[stus.length] = Stu("小I","女",20,"大一");
stus[stus.length] = Stu("小J","男",20,"大三");
for(var i=0;i<stus.length;i++){
if(stus[i].sex=="女" && stus[i].grade =="大一"){
document.write(stus[i].name+"<br/>");
}
</script>
</head>
<body>
</body>
</html>
var student=[['小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<10;i++)
if(student[i][3]=="大一"&&student[i][1]=="女")
{document.write(student[i]+"<br>");}
我这个比较简单,一次求出,没依次算大一和女生
这个有点复杂了你的, 只需要 for(var i=0;i<infos.length;i++) { if(infos[i][3]=="大一") { if(infos[i][1]=="女") { document.write("大一女生的姓名为:"+infos[i][0]+"<br>") } } } 一个循环,和if判断嵌套就可以了,你这样有点麻烦了
JavaScript进阶篇
468061 学习 · 21891 问题
相似问题