Search Javascript array elements

Asked

Viewed 1,321 times

0

I would like to create a input that receives a value informed by the user and that when clicking on a button he executed a function that checks if the value entered exists in my array and if there is what index is contained.

var vetor = [1, 2, 3];<br>
var elemento = document.getElementById("verifica").value;

<.input type='text' id="verifica">

2 answers

1

You can do it this way: The method indexOf seeks the value (val) in the vector (vetor) if found it returns the position of the element in the array. If it does not find the value it returns -1. An array starts at position 0. I made a sum in the result to increase the positions in a house. So the positions will be greater than zero. It is important to observe the function parseInt() it transforms into integer what is passed as parameter. Which is useful in this example because we want to perform comparisons and operations between integers.

var verifica = function(){
    var vetor = [1, 2, 3];
    var val = document.getElementById("verifica").value;
   
    var a = vetor.indexOf(parseInt(val));

    if(a === -1){

      alert("Valor não encontrado")

    }else if(a !== -1){

      alert("Valor encontrado na posição " + (a+1));

    }

  }
 <input id="verifica" type="text" >
  <button type="submit" onclick="verifica()">Verificar
  </button>

1

Use a loop to iterate the array elements and if any value matches the input value return the index of that element.

    var vetor = [1, 2, 3, 4, 5];

    function checar(){
      valInput=document.getElementById("verifica").value;
      for (var i = 0; i < vetor.length; ++i) {
          if (vetor[i] == valInput) {
              index = i;
              console.log(index);
              break;
          }
      }
    }
    <input id="verifica" type="text">
    <button type="submit" onclick="checar()">Verificar</button>

Can use findIndex(), but does not work on IE 11 and above

  • 2

    Negative is easy, just a click, difficult is to post a better answer and then feel free to negativize others. In the land of the gringos this script got 32 ups. Anyway here land Brasilis things are different.

  • Unfortunately something is happening, all the proposed solutions have been voted against. Maybe you haven’t tested any of the solutions proposed here. And you’re just voting against all the answers. Perhaps the question should be excluded.

  • @Felipe Unfortunately, here are people who keep looking defective in the scripts of others but does not present solution/ suggestion/ constructive criticism.

  • is .. I also had my negative. But with good reason. I wrote a weak text and a code with an error in the conditional structure. This morning I noticed the error in the reply and edited it. I feel that this is really bad. There are people who disqualify any question the more the answers. Fiery ego is the name of it.

  • https://chat.stackexchange.com/rooms/75640/votos-negativos-sem-explicacao

Browser other questions tagged

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