Most efficient way to create a zero filled JavaScript array?

前端 未结 30 1870
花落未央
花落未央 2020-11-22 05:58

What is the most efficient way to create an arbitrary length zero filled array in JavaScript?

30条回答
  •  佛祖请我去吃肉
    2020-11-22 06:06

    Although this is an old thread, I wanted to add my 2 cents to it. Not sure how slow/fast this is, but it's a quick one liner. Here is what I do:

    If I want to pre-fill with a number:

    Array.apply(null, Array(5)).map(Number.prototype.valueOf,0);
    // [0, 0, 0, 0, 0]
    

    If I want to pre-fill with a string:

    Array.apply(null, Array(3)).map(String.prototype.valueOf,"hi")
    // ["hi", "hi", "hi"]
    

    Other answers have suggested:

    new Array(5+1).join('0').split('')
    // ["0", "0", "0", "0", "0"]
    

    but if you want 0 (the number) and not "0" (zero inside a string), you can do:

    new Array(5+1).join('0').split('').map(parseFloat)
    // [0, 0, 0, 0, 0]
    

提交回复
热议问题