js快速的に配列を生成する方法

1711 ワード

//     :    
var arr1 = new Array(100);
for(var i=0;i arr1[i] = i;
}
console.log(arr1);


// :push
var arr2 = new Array();
for(var i=0;i<100;i++){
arr2.push(i);
}
console.log(arr2);



// :while
var arr3 = new Array();
var i = 0;
while(i<100){
arr3.push(i);
i++;
}
console.log(arr3);



// :do while
var arr4 = new Array();
var i = 0;
do{
arr4.push(i);
i++;
}
while(i<100)
console.log(arr4);


// :
var arr5 = Object.keys(Array.apply(null, {length:100})).map(function(item){

return +item;

});
console.log(arr5);


// :
var arr6 = Array.from({length:100}, (v,k) => k);
console.log(arr6);


// :
var arr7 = Array.from(Array(100), (v,k) =>k);
console.log(arr7);


// :
var arr8 = new Array(100).keys();
console.log(Array.from(arr8));


// :
var arr9 = [];
var i = 0;
var timer = setInterval(function(){
arr9[i] = i++;
if(i>=100){
clearInterval(timer);
console.log(arr9);
}
},1);


// :
var arr = [];
var i = 0;
function MakeArray(num){
if(i arr[i] = i++;
MakeArray(num);
}
return arr;
}
console.log(MakeArray(100));


// :
var arr11 = new Array(100).toString().split(‘,‘).map(function(item,index){
return index;
});
console.log(arr11);
転載先:https://www.cnblogs.com/fuzitu/p/10723869.html