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

228
Views
How to avoid nested loop using TypeScript

I am working on application where I want to get the user names from employeeList array. Here is I am getting it by using nested loop but this not good practice. Here is my code

let _empList = await this.empService.getEmployeesList();
let taskEvents = await this.taskService.renderedTaskGridData(
  _tasksData,
  undefined
);

let associatesList = [];
for (let proj of this.projectsData) {
  let projTasks = [...taskEvents.filter((x) => x.ProjectId === proj.Id)];

  _empList.forEach((x) => {
    projTasks.forEach((y) => {
      y.AssociatedUsersId.forEach((z) => {
        if ((x.Id = z)) {
          associatesList.push(`${x.FirstName} ${x.LastName}`);
        }
      });
    });
  });
}

I need to get the list using best practice.

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

0

I'm going to go ahead and assume that the problem you are concerned with is the asymptotic complexity (ie - how much your runtime increases relative to the size of your input).

The way I'd solve that would be to make a time space trade-off, like so:

let associatesList = [];
const projectIds = new Set(this.projectsData.map((proj) => proj.Id));

const userIds = new Set(
  taskEvents
    .filter((task) => projectIds.has(task.ProjectId))
    .flatMap((event) => event.AssociatedUsersId)
);
const employees = _empList
  .filter((employee) => userIds.has(employee.Id))
  .map((employee) => `${employee.FirstName} ${employee.LastName}`);
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!