Whats the difference between Array.fill and a for loop to create Array

后端 未结 2 990
忘掉有多难
忘掉有多难 2020-12-07 00:11

I\'m creating a dungeon crawler game using React.js and I was initializing the board using Array.fill(0). but when I set an element inside the 2d array it sets the entire Ar

2条回答
  •  佛祖请我去吃肉
    2020-12-07 00:35

    You can fill 2D array with a help of Array.fill in a such way:

    let arr = Array(5).fill(0).map(x => Array(5).fill(0))
    

提交回复
热议问题