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

114
Views
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 answers
Answer question

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 Report

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 Report

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 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!