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

120
Vistas
Can I use another loop inside forEach in Javascript? or is it best i use to separate loops?

I am tasked to write a function that returns the missing uniform pieces of Company A or Company B. The function should look thru the array to determine which Company is the one with the missing piece. Can I use another loop inside forEach in Javascript? or is it best i use to separate loops? Any hints are appreciated.

function findCompanyName(uniformSet, uniformPieces) {
  uniformPieces.forEach((element) => {
    const splitedArrayUniPcs = element.split("_");
  });

  //return "example";
}

findCompanyName(
  ["shirt", "pants"],
  ["companyA_shirt", "companyA_pants", "companyB_shirt"]
); //return companyB

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

0

Can I use another loop inside forEach in Javascript?

Yes! There is nothing wrong with nested loops in Javascript.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Technically there's nothing wrong with it but you'll quickly find yourself confused. Instead, use named functions instead of in-line anonymous functions. That way you can debug and maintain your code much more easily in the future because the looping code will read nicely as long as your function names make sense, and you can reason through each function without having to keep the entire Jenga tower in your head.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Can I use another loop inside forEach in Javascript?

Yes, you can use another loop inside a forEach loop in Javascript.

As @rowan-freeman stated, nested loops (loops inside a loop) are completely allowed in Javascript, as in most other programming languages.

Or is it best I use separate loops?

This depends on the nature of the problem. If you want/need to perform the same loop-logic on all elements of forEach, then nesting the loop is a clean solution.

If you need to run a single pass on all the elements as a post-processing step, for example, that would be a case when a second loop would make sense.

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