var mun =[['小A','女','21','大一'],['小B','男','23','大三'],['小C','男','24','大四'],['小D','女','21','大一'],['小E','女','22','大二'],['小F','男','21','大一'],['小G','女','22','大二'],['小H','女','20','大三'],['小I','女','20','大一'],['小J','男','20','大三']];
for (i=0;i<=10;i++){
if((mun[i][3]=='女') && (mun[i][1]=='大一')){
document.write(mun[i][0]);
}
}
请问这个为什么报错呀?
两个问题:for循环里面 i<10,不是i<=10,因为一共十个元素,最后一个元素的位置索引为9.
第二个问题就是 判断,应该是mun[i][1]=="女"&&mun[i][3]=="大一"。
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
</head>
<body>
<script type="text/javascript">
var mun =[['小A','女','21','大一'],['小B','男','23','大三'],['小C','男','24','大四'],['小D','女','21','大一'],['小E','女','22','大二'],['小F','男','21','大一'],['小G','女','22','大二'],['小H','女','20','大三'],['小I','女','20','大一'],['小J','男','20','大三']];
for (i=0;i<10;i++){
if((mun[i][1]=='女') && (mun[i][3]=='大一')){
document.write(mun[i][0]);
}
}
</script>
</body>
</html>
if((mun[i][1]=='女') && (mun[i][3]=='大一'))
条件写错了