可爱到爆炸
2018-05-30 22:58
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<4;i++) { infos[i] = new Array(); for (var j = 0; j < 10; j++) { //第一次筛选,找出都是大一的信息 if (infos[i][3] = '大一' && infos[i][1] = '女') { //第二次筛选,找出都是女生的信息 document.write(infos[i]); } }
<!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,"大三"]]; //第一次筛选,找出都是大一的信息 var dayi = new Array(); var sub = 0; document.write("大一学生有:"); for(var i=0; i<10; i++) { if( infos[i][3] == "大一") { dayi[sub++] = i; document.write(infos[i][0]+","); } } document.write("</br>女生为:"); //第二次筛选,找出都是女生的信息 for( var i=0; i<dayi.length; i++) { sub = dayi[i]; if( infos[sub][1] == "女") document.write(infos[sub][0]+","); } </script> </head> <body> </body> </html>
document.write(infos[i]); 这条代码里的 infos[i] 写成infos[i][0]
<script language="javascript">
var myarray=[['小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<myarray.length;i++)
{
for(var j=0;j<4;j++)
{
if(myarray[i][3]!="大一")
{ continue;
}
else if(myarray[i][1]!="女")
{continue;}
document.write(myarray[i][j]);
}
}
</script>
受教了
数组定义不对,应该用中括号[ ], 二维数组写反了顺序是先行后列,这个数组应该是10行4列,也就是说 i<10, j<4;
var infos=[
['小A','女',21,'大一'], ['小B','男',23,'大三'],['小C','男',24,'大四'], ['小D','女',21,'大一'],['小E','女',22,'大四'], ['小F','男',21,'大一'],['小G','女',22,'大二'], ['小H','女',20,'大二']];
for(var i=0; i<infos.length; i++){
for(var j=0, j<4; j++){
if(infos[i][3] =="大一" && infos[i][1]=="女"){
document.write(infos[i][j]+“,”);
}
}
document.write("<br/>");
}
JavaScript进阶篇
468061 学习 · 21891 问题
相似问题