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

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

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