I create an empty 3x3x1 3D array
[
[[],[],[]],
[[],[],[]],
[[],[],[]]
]
Now, The element at position arr[1][1]
is []
, So, If I execute arr[1][1].push(1)
it should insert 1
on position arr[1][1]
There are 2 methods for creating an empty 3x3x1 array in JS, Here is the code,
var arr1 = [...new Array(3)].map(e => [...new Array(3)].fill([]));
var arr2 = [[[],[],[]],[[],[],[]],[[],[],[]]];
arr1[1][1].push(1);
arr2[1][1].push(1);
console.log(arr1)
console.log(arr2)
ie. Through shortcut and another manually, both arr1
and arr2
should be identical, so should be the output, But the output is as follows,
[[[], [], []], [[1], [1], [1]], [[], [], []]]
[[[], [], []], [[], [1], []], [[], [], []]]
Why is it that first array gives such output? Aren't both identical?
I want the output to be of the 2nd form, If this is a wrong way to create an empty 3x3x1 array, please suggest a method such that it gives expected output.