Array.prototype.fill() with object passes reference and not new instance

后端 未结 6 1527
陌清茗
陌清茗 2020-11-22 03:57

I was toying a bit and was trying to instantiate a new array of length x, where all elements of that array were initialized to a value y:

         


        
6条回答
  •  滥情空心
    2020-11-22 04:13

    Ilias Karim's answer is most excellent. I just did the following:

    a = Array.from({length:l}, () => new Array(c).fill(prefix));
    

    to create a pre-filled 2D array of the specified size, l by c, filled with prefix. Now my code can fill in the slots in the 2D matrix that need non-prefix values.

提交回复
热议问题