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

199
Vistas
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 Respuestas
Responde la pregunta

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