new Array(n) will create an array of n * undefined, so why can't you use new Array(n).map((_, index) => index) function to get [0, 1, 2 ..., n-1 ] What about arrays like this?
I know that new Array(n).fill(0).map((_, index) => index) is ok, but is there any essential difference between the two arrays of n * undefined and n * 0 ?
Common pits for initializing an n*m two-dimensional array:
- Wrong way 1:
new Array(n).fill(new Array(m).fill(0))All arrays point to the same reference - Wrong way 2:
new Array(n).map(v => new Array(m).fill(0))"ghost array" problem - Correct way 1:
new Array(n).fill(0).map(v => new Array(m).fill(0))