<!DOCTYPE HTML> <html > <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>流程控制语句</title> <script type="text/javascript"> var infos=[["小A","女",21,"大一"],["小B","男",23,"大三"],["小C","男",24,"大四"],["小D","女",21,"大一"],["小E","女",22,"大四"],["小F","男",21,"大一"],["小G","女",22,"大二"],["小H","女",20,"大三"],["小I","女",20,"大一"],["小J","男",20,"大三"]]; //第一步把之前的数据写成一个数组的形式,定义变量为 var arr1 = []; var n = 0; for(var i=0;i<infos.length;i++){ if( infos[i][3] == "大一" ){ arr1[n] = infos[i]; document.write(arr1[n]+"<br/>"); n=n+1; } } document.write("大一人数: "+arr1.length+"<br/>"); //第二次筛选,找出都是女生的信息 for(var i=0;i<arr1.length;i++){ //这里可以用switch if(arr1[i][1]=='女') { document.write(arr1[i][0]+"<br/>");} } //第一次筛选,找出都是大一的信息 //第二次筛选,找出都是女生的信息 </script> </head> <body> </body> </html>
关于里面的n=n+1 不是太明白他的意思 输出infos【i】后相当于输出了1个数组 既然arr1【n】=infos【i】那么arr1不就是这个数组了吗?
有四个人,编号分别是1、2、3、4,每人给一个苹果。先给了一个给1号,如果给下一个人时,不把编号加1,不就一直给同一个人了吗
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<=10;i++){
if(infos[i][3]=="大一"&&infos[i][1]=="女"){
document.write(infos[i][0]+"<br>")
}
}
他这个是将筛选出来的所有的大一的学生信息全部存放到另一个数组中,就是arr1,相当于用所有大一的学生信息(包括姓名,性别,年龄,年级)初始化arr1数组,再在arr1中筛选性别为 女 的学生的姓名。
n只是代表数组的下标,也就只是数组的键值。
if(infos[i][3] == "大一"){ arr1[n] = infos[i]; document.write(arr1[n]+"<br/>"); n=n+1; }
n = n + 1的意义在于当arr[0] = infos[0]之后将n自加,进行下次循环赋值
arr[1] = infos[3];
arr1也是个数组,它的index=0时,值为arr1[i]的数组.然后n也是要++的. 实际arr1也是个2维数组.