Andy_Kx
2017-04-22 22:00
<!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[('小A','女',21,'大一'), ('小B','男',23,'大三'), ('小C','男',24,'大四'),('小D','女',21,'大一'), ('小E','女',22,'大四'), ('小F','男',21,'大一'),('小G','女',22,'大二'),('小H','女',20,'大三'), ('小I','女',20,'大一'),('小J','男',20,'大三') ]
//第一次筛选,找出都是大一的信息
var i;
for(i=0;i<infos.length;i++){
if(infos[i][3]=='大一'){
document.write("大一的学生有:"+infos[i][0]+"<br>");
}
}
//第二次筛选,找出都是女生的信息
var j;
for(j=0;j<infos.length;j++){
if(infos[j][3]=='大一'&&infos[j][1]=='女'){
document.write("大一女生有:"+infos[i][0]+"<br>");
}
}
</script>
</head>
<body>
</body>
</html>
var infos=[["小A","女",21,"大一"], ["小B","男",23,"大三"], ["小C","男",24,"大四"], ["小D","女",21,"大一"], ["小E","女",22,"大四"], ["小F","男",21,"大一"], ["小G","女",22,"大二"], ["小H","女",20,"大三"], ["小I","女",20,"大一"], ["小J","男",20,"大三"] ]; for (i = 0;i<=infos.length;i++){ if(infos[i][3] == "大一" && infos[i][1] == "女"){ document.write(infos[i][0]+"<br/>"); } }
声明数组那出现了问题,具体请去看数组声明和多维数组那两节课 。
还有就是到这还没有学过json,楼上那个只会让你越看越懵逼,不用去管他。
数组不可以这样定义的,数组里每一项应该作为一个对象比较合适,你看一下
<!DOCTYPE HTML>
<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>流程控制语句</title>
<script type="text/javascript">
//第一步把之前的数据写成一个数组的形式,定义变量为 infos
var infos=[
{name:'小A',sex:'女',age:21,grade:'大一'},
{name:'小B',sex:'男',age:23,grade:'大三'},
{name:'小C',sex:'男',age:24,grade:'大四'},
{name:'小D',sex:'女',age:21,grade:'大一'},
{name:'小E',sex:'女',age:22,grade:'大四'},
{name:'小F',sex:'男',age:21,grade:'大一'},
{name:'小G',sex:'女',age:22,grade:'大二'},
{name:'小H',sex:'女',age:20,grade:'大三'},
{name:'小I',sex:'女',age:20,grade:'大一'},
{name:'小J',sex:'男',age:20,grade:'大三'}]
//第一次筛选,找出都是大一的信息
var i;
for(i=0;i<infos.length;i++){
if(infos[i]['grade']=='大一'){
document.write("大一的学生有:"+infos[i]['name']+"<br>");
}
}
//第二次筛选,找出都是女生的信息
var j;
for(j=0;j<infos.length;j++){
if(infos[j]['grade']=='大一'&&infos[j]['sex']=='女'){
document.write("大一女生有:"+infos[j]['name']+"<br>");
}
}
</script>
</head>
<body>
</body>
</html>
上面这么定义数组也可以吧
JavaScript进阶篇
468061 学习 · 21891 问题
相似问题