1.通过for循环的方式
var arr=[];for(let i=0;i<40;i++)arr.push(i);
//或
var arr=[];for(let i=0;i<40;i++)arr[i]=i;
2.使用Array.from
参考资料https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/from
var arr=Array.from({length:20},(v,i)=>i);
3.使用Array.apply
参考资料 https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Function/apply
Array.apply(null,{length:20}).map((v,i)=>i);
4.使用Array.prototype.fill()
参考资料 https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/fill
var arr=Array(40).fill().map((v,i)=>i);
网友评论