Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

222
Visualizações
Remove object from array after being used in foreach function

Goodmorning,

I would like to remove an object from an array after it is used in a forEach loop. And then the foreach loop should continue with the next object left in the array.

However right now it runs the function for the first object, then I remove this first object and then the function stops running.

This is my script:

function test() {
  // Create array with id's of mailings to get from database
  var allAutomations = [
    {"id": "52"},
    {"id": "72"}
  ] 

  allAutomations.forEach(function(automation, index) {

    var a = 1;
    while (a <= 10) {
      console.log(a);
      a++
    }

    allAutomations.splice(index, 1);

    console.log(allAutomations);
          
  });
}

test();

As you can see in console at removing the current object works, but after that it should run the while loop for the one with id '72' again.

Anybody knows how to fix this?

about 4 years ago · Santiago Trujillo
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda