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

后端 未结 6 1529
陌清茗
陌清茗 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:18

    You could also solve this by the following workaround.

    var arr = new Array(2).fill({});
    arr = JSON.parse(JSON.stringify(arr));
    

提交回复
热议问题