Mapping Array in Javascript with sequential numbers

后端 未结 4 1863
逝去的感伤
逝去的感伤 2021-02-04 17:40

The following code:

let myArray = Array.apply(null, {length: 10}).map(Number.call, Number);

Creates the following Array:

[0, 1,         


        
4条回答
  •  清歌不尽
    2021-02-04 18:25

    es6 simpliedfied version

    let k = Array.from({ length: 5 }).map((currentElement, i) => i)
    
    console.log(k)
    
    // Output -[0, 1, 2, 3, 4]

提交回复
热议问题