How to run a search on an array until a condition is met?

Asked

Viewed 66 times

2

I have an array of users coming from a firebase database, I capture this array and run a map to capture some information, for example: mount a Cpf array, phone... This Cpf array for example I will use to compare with the Cpf that the client inserts at the time of its registration, to block the registration with an existing Cpf. I tried to use the for to do this, but the for goes through the list, when it reaches the equal condition (finds the repeated Cpf) it returns a true, but as it continues running the list it returns a false soon after, because the other cpfs are different from this.

I would like to know how I can perform this comparison until the condition is met? For example, I want to do the go until repeated Cpf is found, return me the true and I’ll handle the rest, what I’ve done so far was.

Here I do the map returning email, Cpf, phone, name...

  this.pacientesSubscription = this.pacienteProvider.buscarPacientes().subscribe(data => {
  this.cpfsCadastrados = data.map(result => {
    return result.cpfSolicitante;
  })

  this.rgsCadastrados = data.map(result => {
    return result.rgSolicitante;
  })

  this.emailsCadastrados = data.map(result => {
    return result.emailSolicitante;
  })

  this.telefonesCadastrados = data.map(result => {
    return result.telefone;
  })

  this.nomesCadastrados = data.map(result => {
    return result.nomeSolicitante;
  })
})

Then here I try to compare. What is nothing more, enter the list of names, if the name at the position i is equal to the one typed, return me a true in the startName variable. But it keeps going through the list and reaches the nameRepetido = false. Because the other names in the list are different.

consultaNome(){
for(let i = 0; i < this.nomesCadastrados.length; i++){
  if(this.nomesCadastrados[i] === this.paciente.nomeSolicitante){
    this.nomeRepetido = true;
    alert('Já existe um cadastro com este nome na nossa plataforma, por favor corrija este campo');
  } else {
    this.nomeRepetido = false;
  }
  console.log(this.nomeRepetido);
}

}

3 answers

0


I see no need to always set the startName to false, if you set it to false, you only need to change 1x..

When the condition is true you can put the i = this.nomesCadastrados.length

  • 1

    Thanks man, your answer helped me solve here, I did as you said.

0

Follow the example of how the final result was, also includes another data node for comparison:

consultaNome(){
this.nomeRepetido = false;
for(let i = 0; i < this.nomesCadastrados.length; i++){
  if(this.nomesCadastrados[i] === this.paciente.nomeSolicitante){
    this.nomeRepetido = true;
    i = this.nomesCadastrados.length;
    alert('Já existe um cadastro com este nome na nossa plataforma, por favor corrija este campo');
  }
  console.log(this.nomeRepetido);
}

for(let i = 0; i < this.nomesProfissionaisCadastrados.length; i++){
  if(this.nomesProfissionaisCadastrados[i] === this.paciente.nomeSolicitante){
    this.nomeRepetido = true;
    i = this.nomesCadastrados.length;
    alert('Já existe um cadastro com este nome na nossa plataforma, por favor corrija este campo');
  }
  console.log(this.nomeRepetido);
}
}

0

Another alternative is that you can instead use a for simple, use a more advanced method such as filter, that returns a Boolean as the return of a condition tested by it:

public nomesCadastrados = [];
public repetido: boolean;
public valorDigitadoPeloUsuario: any;

this.pacientesSubscription = this.pacienteProvider.buscarPacientes().subscribe(data => {
  this.cpfsCadastrados = data.map(result => {
    return result.cpfSolicitante;
  })

  this.rgsCadastrados = data.map(result => {
    return result.rgSolicitante;
  })

  this.emailsCadastrados = data.map(result => {
    return result.emailSolicitante;
  })

  this.telefonesCadastrados = data.map(result => {
    return result.telefone;
  })

  this.nomesCadastrados = data.map(result => {
    return result.nomeSolicitante;
  })
})

consultaNome(){
  this.nomesCadastrados.filter((elemento: any) => {
    if(elemento == this.valorDigitadoPeloUsuario) {
      this.repetido == true;
      alert('Já existe um cadastro com este nome na nossa plataforma, por favor corrija este campo');
    }
  })
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.