从一个已有数组中向另外一个空白数组添加元素
var adressTable=new Array("image/01.png","image/02.png","image/03.png","image/04.png","image/05.png","image/06.png","image/07.png","image/08.png","image/09.png","image/10.png","image/11.png","image/12.png","image/13.png","image/14.png","image/15.png"); var newArray=new Array(); getNum(newArray,adressTable); alert(newArray[0]); function getNum(a,b){ while(a.length<=15){ var index = Math.floor(Math.random() * 15); for(j=0;j<a.length;j++){ if(a[j]==b[index]) { break; } else { var e=b[index]; a.push(e); n++; } } } return a; }
这是想无序添加么?
因为你一开始a.length=0,所以for循环一直不会进else里面
所以a.length一直等于0 毕业论文
都没执行for循环里面的内容,判断下当a.length==0时的添加数组元素
var adressTable=new Array("image/01.png","image/02.png","image/03.png","image/04.png","image/05.png","image/06.png"
,"image/07.png","image/08.png","image/09.png","image/10.png","image/11.png","image/12.png","image/13.png","image/14.png","image/15.png");
var newArray=new Array();
getNum(newArray,adressTable);
alert(newArray[0]);
function getNum(a,b){
while(a.length<=15){
var index = Math.floor(Math.random() * 15);
var ft=false;
for(j=0;j<a.length;j++){
if(a[j]==b[index]){
ft=true;
break;
}
}
if(!ft){
var e=b[index];
a.push(e);
n++;
}
}
return a;
}