<!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","大三"] ]; //第一次筛选,找出都是大一的信息 for(var i=0;i<9;i++){ if(infos[i][3]==="大一"){ document.write(infos[i][0]); } } //第二次筛选,找出都是女生的信息 for(var i=0;i<9;i++){ if(infos[i][1]==="女"){ document.write(infos[i][0]); } } </script> </head> <body> </body> </html>
其实二维数组那个问题也能这么写吧,只是你的代码都是出于一些小问题,待优化:
<!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] == "大一") { if (infos[i][1] == "女") { document.write("大一的女生有:" + infos[i][0] + "<br>"); } } } </script> </head> <body> </body> </html>
首先,把for循环里的i<9替换成i<infos.length;其次,你这个还没变成二维数组;再来,你的if判断里有三个=....应该改成两个=;还有一点小问题,数组里的数字可以不加引号,,还有你的代码重复了,应该把两个for循环里的内容合并起来,if判断按照题目来应该嵌套