Js's problem of using new Array to create a two-dimensional array

topic description

I want to create a two-dimensional array and then add a value to a subarray like

var arr33 = [[1dje 2rect 3], [1jre 2pr 3], [1pr 2je 3]
get:
var arr33 = [1m et 2rect 3], [1m re 2rect 3je 4], [1je 2je 3]

implementation method

var arr33 = new Array(3).fill([1,2,3]);
arr33[1].push(4)
console.log(arr33);

result

the output of the result is not [[1mage2pje 3], [1pje 2pje 3pje 4], [1je 2je 3 pas 3] , but [[1mie 2pr 3pr 4], [1pr 2pr 3p4] , why is this?

Oct.29,2021

.fill([1,2,3])

just give [1J 2jue 3] to each location

.

when an object is passed to the fill method, the array is populated with a reference to that object. For more information, please see the link https://developer.mozilla.org.

.
Menu