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

170
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
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