How do I browse an Array in Javascript?

Asked

Viewed 504 times

-1

I’d like to know how I get around a Array and return its value.

In this case, it would be to compare whether a word/letter typed in a input has already been inserted before by another input and if it was, it would have to be removed.

1 answer

1

Here is an example using includes to check whether or not the array value exists.

Other :

W3

Example using foreach :

W3 FOREACH

var palavras = ["mateus", "veloso", "vermelho"];
$(function(){
  $('#bt').click(function(){
    let valor = $('#teste').val();
    if(palavras.includes(valor)){
      alert('palavra encontrada no array');
    }else{
      alert('palavra não encontrada no array');
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="teste">
<input type="button" value="verificar" id="bt">

Browser other questions tagged

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