How do I go through an Array in JavaScript? [closed]

-1

I wanted to know how to go through a Array and return the value of it.

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

    
asked by anonymous 08.08.2017 / 21:03

1 answer

1

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

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><inputtype="text" id="teste">
<input type="button" value="verificar" id="bt">
    
08.08.2017 / 21:21