Let's break down the line:
Array(this.rows)
this creates an array with this.rows many rows. In this case, 30.
.fill()
fills the array with undefined values (more info on fill function)
.map(callbackFunction)
this returns a new array with each value being transformed by the function. Since you have an array of undefined, you'll call the function as you would the following callbackFunction(undefined).
Now for the callback function:
() => Array(this.cols).fill(false);
This function takes no parameters (hence ()), and returns an Array with this.cols size (which is 50), all containing false.
tl;dr:
So, you're essentially creating a 30x50 matrix filled with false on each element.
EDIT:
explaining arrow functions:
(list-of-parameters) => (function block | return value)
To explain using examples, we can transform function one() { return 1; } into () => 1;.
Or function times(a, b) { return a * b;} into (a, b) => a * b;
Or another:
let x = 0;
function foo(y) {
const returnValue = (x++) * y;
return returnValue;
}
to
let x = 0;
const foo = (y) => {
const returnValue = (x++) * y;
return returnValue;
}
EDIT2:
More ways to accomplish the same result:
let result = Array(rowCount).fill();
for (let i = 0; I < rowCount; i++) {
result[i] = Array(colCount).fill(false);
}
Another:
const line = Array(colCount).fill(false);
const result = Array(rowCount).fill().map(() => [...line]);
And another:
const line = Array(colCount).fill(false);
const result = [];
for (let idx = 0; idx < rowCount; idx++) {
result.push([...line]);
}
Or you can create your own "matrix creator":
function matrix(row, col) {
const data = Array(row * col).fill(false);
const findIdx = (x, y) => y * col + x;
return {
get: (x, y) => data[findIdx(x,y)],
set: (x, y, value) => {
data[findIdx(x,y)] = value
return data[findIdx(x,y);
},
};
}
.fill()and.map()individually?