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

79
Views
Use array find() method instead of for loop

How can we use Array.find() method instead of for loop in this code ?

onLoadTickets() {
    const ticketsReq = this.ticketService.getTickets();
    const tariffsReq = this.tariffService.getTariffs();

    forkJoin([ticketsReq, tariffsReq]).subscribe(results => {
      const data = results[0];
      const tariffResp = results[1];
      this.tickets = data.requests;
      for (let i = 0; i < this.tickets.length; i++) {
        for (let j = 0; j < tariffResp.tariffs?.length; j++) {
          if (tariffResp.tariffs[j].id == this.tickets[i].tariffId) {
            this.tickets[i].tariff = tariffResp.tariffs[j]
          }
        }
      }
    });
  }

Note :

Using find() method is not mandatory. I have to write this code with any array methods.

Edit : I have used map() and includes() methods. my solution:

const tariffIds = tariffResp.tariffs.map((tariff: Tariffs) => tariff.id);
      this.tickets.map((item) => {
        if (tariffResp.tariffs === null || tariffResp.tariffs === undefined) {
          return item;
        }
        if (tariffIds.includes(item.tariffId)) {
          item.tariff = tariffResp.tariffs[tariffIds.indexOf(item.tariffId)];
        }
        return item;
      });

This works but I'm not sure it's the best solution

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

0

Array.find() method returns the first element in the provided array that satisfies the provided testing function.

So, if in your case you only have one tariff against all the tickets then you can go ahead with Array.find() but if you have multiple tariff and multiple tickets then you can go ahead with Array.filter().

Demo with Array.find() :

const tickets = [{
    tariffId: 1,
  name: 'Ticket 1'
}, {
    tariffId: 2,
  name: 'Ticket 2'
}];

const tariffResp = {
    tariffs: [{
    id: 1
  }]
};

const result = tickets.find((obj) => tariffResp.tariffs[0].id);

console.log(result);

Demo with Array.map() along with Array.filter() :

const tickets = [{
    tariffId: 1,
  name: 'Ticket 1'
}, {
    tariffId: 2,
  name: 'Ticket 2'
}, {
    tariffId: 3,
  name: 'Ticket 3'
}];

const tariffResp = {
    tariffs: [{
    id: 1
  }, {
    id: 2
  }]
};

const result = tariffResp.tariffs.map((obj) => {
    return tickets.filter((ticketObj) => obj.id === ticketObj.tariffId);
});

console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

this.tickets=data.request.map(x=>{
  const obj:any=x;
  obj.tariff=tariffResp.find(t=>t.id==x.tariffId)
  return obj
})

You loop over data.request using map. map transform an array in another

First you create an object with the values of x

After you add a new propety "tariff" that is the "tariffResp" who has the "id" property equal to the property "tariffId" of x

Check find and map methods of an array

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!