Most efficient way to create a zero filled JavaScript array?

前端 未结 30 1646
花落未央
花落未央 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:19

    Didn't see this method in answers, so here it is:

    "0".repeat( 200 ).split("").map( parseFloat )
    

    In result you will get zero-valued array of length 200:

    [ 0, 0, 0, 0, ... 0 ]
    

    I'm not sure about the performance of this code, but it shouldn't be an issue if you use it for relatively small arrays.

提交回复
热议问题