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

后端 未结 6 1560
陌清茗
陌清茗 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条回答
  •  猫巷女王i
    2020-11-22 04:27

    You can first fill the array with any value (e.g. undefined), and then you will be able to use map:

    var arr = new Array(2).fill().map(u => ({}));
    
    var arr = new Array(2).fill().map(Object);
    

提交回复
热议问题