JS创建长度为100的数组,数值为⾓标<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script language="javascript" type="text/javascript">
//实现⽅法⼀:循环赋值
var arr1 = new Array(100);
for(var i=0;i<arr1.length;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<num){
arr[i] = i++;
MakeArray(num);
}
typeof array
return arr;
}
console.log(MakeArray(100));
//实现⽅法⼗⼀:
var arr11 = new Array(100).toString().split(',').map(function(item,index){ return index;
});
console.log(arr11);
</script>
<title>javascript typeof  </title>
</head>
<body>
</body>
</html>

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。