I am trying to parse through a 2D array, remove empty cells, and then push it to a new 2D array. When I do the .push([]) step before the IF statement, everything works fine but unfortunately that pushes an empty array every iteration of the for loop. When I try to put that line within the If statement, I get the following error: TypeError: Cannot read property 'push' of undefined.
This doesn't work:
var i=0;
var oA = [];
for(i; i<bRows;i++)
{
if(nbaValues[i][0]){
oA.push([]);
for(var j=0;j<bCol;j++){
oA[i].push(nbaValues[i][j]);
}
}
}
Logger.log(oA);
While this works:
var i=0;
var oA = [];
for(i; i<bRows;i++)
{
oA.push([]);
if(nbaValues[i][0]){
for(var j=0;j<bCol;j++){
oA[i].push(nbaValues[i][j]);
}
}
}
Logger.log(oA);
Thanks in advance!
Does this help?
const has_empty = ['a',,'b',,'c' ];
console.log(has_empty); // output ['a', undefined, 'b', undefined, 'c']
const no_empty = has_empty.filter(item => item);
console.log(no_empty); // output ['a', 'b', 'c']