I am simply trying to initialize a 2D array of size N x N - where N = the boardSize prop - but when I write out the following, I instead get a 2D array of size N x N^2:
let cells = new Array(this.props.boardSize).fill([]).map((e) => {
let newRow = e;
for (let i = 0; i < this.props.boardSize; i++) {
newRow.push(this.intializeCell());
}
return newRow;
});
Why is this behavior happening? Is it because I'm initializing an Array of size 6 with blank Array objects? Or is it some weird behavior I'm not noticing going on in the for loop?
I will mention that when I alter the code to the following, it works as intended:
let cells = new Array(this.props.boardSize)
.fill(new Array(this.props.boardSize).fill(0))
.map((row) => {
let newRow = row.map((_) => this.intializeCell());
return newRow;
});