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

197
Views
Optimize code in JavaScript (Currently using too many for loops)

I have been working on a data formating question, and finally got the answer.

However, my answer has too many loops, and it will slow down time complexity when implementing.

Below is my code:

const userInfo = [];

for (let i = 0; i < projects.length; i++) {
  const userProject = projects[i].userProjects;

  for (let j = 0; j < userProject.length; j++) {
    const userExist = userInfo.some((item) =>
      item.userId.includes(userProject[j].user.userId)
    );
    if (!userExist) userInfo.push(userProject[j].user);
  }
}

userInfo.forEach((user) => {
  let tempArr = [];
  for (let i = 0; i < projects.length; i++) {
    const userProject = projects[i].userProjects;
    for (let j = 0; j < userProject.length; j++) {
      if (user.userId === userProject[j].userId)
        tempArr.push({
          projectId: projects[i].projectId,
          name: projects[i].name,
        });
    }
  }
  user.project = tempArr;
});

console.log(userInfo);

Is there a way I can optimize my code? I tried to combine the nested for loops together but not working...

Please help. Thank you so much!!!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

In the top part, the outer two loops look necessary (since you have N projects * M userProjects), but the third .some isn't - change the userInfo from an array to a mapping of userIds to their users, so inside the second loop, you only have to look up to see if the userId already exists (O(1)), and assign if it doesn't.

Instead of assigning the user, assign an object of the shape you need at the end - a [{ projectId, name }]. This'll save you from having to go through the whole array again - once you've checked to see if you need to create the surrounding array for the userId, you can push the object to the array whether or not you had to create the array on that iteration or not.

const userProjectsByUserId = new Map();
for (const { userProjects, projectId, name } of projects) {
    for (const { user } of projects) {
        if (!userProjectsByUserId.has(user.userId)) {
            userProjectsByUserId.set(user.userId, { user, projects: [] }]);
        }
        userProjectsByUserId.get(user.userId).projects.push({ projectId, name });
    }
}

This'll give you a Map of the shape

userId => {
  user,
  projects: [
    { projectId, name },
    ...

If you need exact data structure your current code is giving you, then afterwards, do:

const output = [...userProjectsByUserId.values()]
  .map(({ user, projects }) => ({ ...user, project: projects }));

(though you might consider using projects instead of project, since it's a collection of projects, not a single project)

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!