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

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

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 Relatório

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