Initializing an Array with a Single Value

后端 未结 10 1180
清酒与你
清酒与你 2020-12-12 23:34

Is there a more compact way to do this sort of initialization?

for (var i = 0; i < arraySize; i++) array[i] = value;
10条回答
  •  無奈伤痛
    2020-12-12 23:51

    This is an old question, but I use this in modern JS:

    [...new Array(1000000)].map(()=>42);
    

提交回复
热议问题