Populating a 2D array in Javascript with random numbers

前端 未结 3 1071
日久生厌
日久生厌 2021-01-27 11:15

I\'m trying to populate a 2D array in javascript with random numbers. Although each column in the array is random, each row is identical which is not what I want (see image bel

3条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-27 11:39

    One way of doing this using map

    let op = new Array(10)
             .fill(0)
             .map(e=>(new Array(5)
             .fill(0)
             .map(e=> Math.floor(Math.random() * 5))))
    
    console.log(op)

提交回复
热议问题