My attempt to search through an array using a for loop is not yielding the expected results.
let matrix = [];
for(let i=0; i<this.row; i++){
for(let j=0; j<this.column; j++){
if(this.grid[i][j].name != ""){
matrix.push(this.grid[i][j].name);
console.log(matrix);
}
}
However, I encounter an issue where I receive an error stating that this.grid[i][j].name
is undefined. Surprisingly, when I adjust the condition as follows:
let matrix = [];
for(let i=0; i<this.row; i++){
for(let j=0; j<this.column; j++){
if(this.grid[i][j]){
matrix.push(this.grid[i][j].name);
console.log(matrix);
}
}
I check for the existence of an object and then proceed to push the name property into the variable matrix
. Oddly enough, the property this.grid[i][j].name
appears to be defined in this scenario. However, I am left wondering why it is not defined within the if statement?