Javascript | Set all values of an array

前端 未结 8 883
清酒与你
清酒与你 2020-12-29 19:45

Code

var cool = new Array(3);
cool[setAll] = 42; //cool[setAll] is just a pseudo selector..
alert(cool);

Result

8条回答
  •  心在旅途
    2020-12-29 19:50

    The ES6 approach is very clean. So first you initialize an array of x length, and then call the fill method on it.

    let arr = new Array(3).fill(9)
    

    this will create an array with 3 elements like:

    [9, 9, 9]
    

提交回复
热议问题