Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

306
Vistas
Push to array not working inside If statement, but works one level higher in for loop. Google Script / JavaScript

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!

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

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']

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda