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

167
Views
When using Array.map() followed by a for loop on that same array, is it double work?

I'm perplexed why a previous engineer wrote some code that looks like double work to me. Could you not just do either the accounts.map() or the for (const acct of accounts)? Why both?

const accounts = await mongohelper.find(conn, 'accounts', query, 2000);
if(!accounts.length){return;}
const arrayofwork = accounts.map(act => salesmanwork(act, conn))


for (const act of accounts) {
    arrayofwork.push(salesmanwork(act, conn));
}

await Promise.allSettled(arrayofwork)
return await recursivecall(workitemin, count);
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

It's making a duplication of the processed values from the accounts array by salesmanwork function. Maybe that's what's intended or might be a bug, some code that they didn't remove.

about 4 years ago · Juan Pablo Isaza Report

0

Output is basically the same, the only differences are speed, readability and functionality, which barely differ. What I mean by this is that when using .map() it returns an array, in where you can use it in one-liners and promises, while in for loops, you need to initialize, then make the loop, and return the same value.

In this case, it is probably some code someone forgot to clean up; either works, the only differences are readability. If you are using a bigger array, use the for loop, otherwise it's up to you.

about 4 years ago · Juan Pablo Isaza Report
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!