Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

67
Visualizações
How do I filter through an array and return the values in that array that match a user input?

I have an HTML file with an input and a button element in the body that looks like this:

<input type="text" name="searchBar" id="searchBar">
<button onclick="returnText()">Submit</button>

Notice that the button will execute the 'returnText()' function when clicked.

Furthermore, my HTML file links to a JavaScript file that contains the following array:

const myData = [
{
    name: "John Miller",
    age: 33,
    location: "Chicago"
},

{
    name: "Jane Doe",
    age: 78,
    location: "Lansing"
},

{
    name: "Jamie Stevens",
    age: 21,
    location: "San Diego"
}
]

Now, I am trying to take the user input and find any string or number that matches any string or number in one of the objects in my array, and display it back to the user. I am stuck trying to figure out how to match the user input with the data in my array. I can do each of these pieces separately, but can't seem to put them together.

The closest I have gotten is what I have below. But this only returns an empty array.

function returnText() {
 let input = document.getElementById('searchBar').value.toLowerCase();
 let filteredNames = myData.filter(e => e.name === input);
 console.log(filteredNames);
 };
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You need to convert the item's name property value to lower case when comparing.

const myData=[{name:"John Miller",age:33,location:"Chicago"},{name:"Jane Doe",age:78,location:"Lansing"},{name:"Jamie Stevens",age:21,location:"San Diego"}];

function returnText() {
  let input = document.getElementById('searchBar').value.toLowerCase();
  let filteredNames = myData.filter(e => e.name.toLowerCase() === input);
  console.log(filteredNames);
};
<input type="text" name="searchBar" id="searchBar" value="John Miller">
<button onclick="returnText()">Submit</button>

If you want to check every property, you can use Object.values to get the property values of the object and check whether one of the values is equal to the input. The benefit of this solution over checking hard coded properties is that if you choose to add an extra property, this will still function properly.

const myData=[{name:"John Miller",age:33,location:"Chicago"},{name:"Jane Doe",age:78,location:"Lansing"},{name:"Jamie Stevens",age:21,location:"San Diego"}];

function returnText() {
  let input = document.getElementById('searchBar').value.toLowerCase();
  let filteredNames = myData.filter(e => Object.values(e).map(e => String(e).toLowerCase()).some(e => e.includes(input)));
  console.log(filteredNames);
};
<input type="text" name="searchBar" id="searchBar" value="Miller">
<button onclick="returnText()">Submit</button>

about 4 years ago · Juan Pablo Isaza Relatório

0

Do you mean something like this ?

const myData = [{name:"John Miller",age:33,location:"Chicago"},{name:"Jane Doe",age:78,location:"Lansing"},{name:"Jamie Stevens",age:21,location:"San Diego"}];

function returnText() {
 let input = document.getElementById('searchBar').value.toLowerCase();

 let filteredNames = myData.filter((e) => {
  return Object.values(e).some((value) => {
   return value.toString().toLowerCase().includes(input);
  });
 });

 console.log(filteredNames);
 };
<input type="text" name="searchBar" id="searchBar" value="John Miller">
<button onclick="returnText()">Submit</button>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda