Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

221
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!