Allow value repetition in JS

Asked

Viewed 34 times

1

In the following section, it is verified whether the value already exists within the vector, if it does not exist it is inserted. I wish I could do this normal check, but allow '0' to repeat. Ex:

 v_patr = [1,2,3,0,4,5,0]; //onde só o '0' pode ser inserido novamente.
 w_valor //é o valor recebido como parâmetro!

    if ((v_vet.indexOf(w_valor) == -1) && (w_valor != ""))
            {               
                v_vet[w_Cont_Qtde] = w_valor;
                w_Cont_Qtde = w_Cont_Qtde + 1;  
            }
  • And where do you use the v_patr? You can’t just add the condition || w_valor === 0?

1 answer

3


You just need to check if the number being entered is 0:

if ( (v_vet.indexOf(w_valor) == -1 && w_valor != "") || w_valor === 0) {               
   v_vet[w_Cont_Qtde] = w_valor;
   w_Cont_Qtde = w_Cont_Qtde + 1;  
}

Browser other questions tagged

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