Initializing an Array with a Single Value

后端 未结 10 1195
清酒与你
清酒与你 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:58

    I think this is nice, short and elegant:

    // assuming `value` is what you want to fill the array with
    // and `arraySize` is the size of the array
    Array(arraySize).fill(value);
    

提交回复
热议问题