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

205
Vistas
How to remove element from array inside for-of loop?

I have a loop on this form in a code base I'm working on.

for(const foo of data) {
    foo.bar = getValue();
}

I want to change this, so that I remove the element foo if a condition based on foo.bar is satisfied. Is this possible within the for-of loop? Or do I need to use another approach? This is my attempt, but I don't feel secure about it. It really looks like one of those things that works until it does not.

for(const x of data) {
    foo.bar = getValue();
    if(foo.bar > 5)
        data.splice(data.indexOf(foo), 1);
}

I know that I could filter the array after creating it. Would that make more sense?

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

0

It makes more sense to filter the array previously to avoid mutating the array you are looping (splice mutates the array).

That way is clearer, easier to debug and you also get to avoid side effects.

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