美文网首页
JavaScript 几种快速生成有序整数数组方法参考

JavaScript 几种快速生成有序整数数组方法参考

作者: 关爱单身狗成长协会 | 来源:发表于2018-10-29 12:01 被阅读37次
    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);
    

    相关文章

      网友评论

          本文标题:JavaScript 几种快速生成有序整数数组方法参考

          本文链接:https://www.haomeiwen.com/subject/xnhbtqtx.html