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

148
Visualizações
Have problem with JavaScript .filter and .find to return items with specific letters

In the following JavaScript code, I am trying to get names that have the letter "Y" or "y". When I looked up how to do that, I saw .filter and .find methods. But this code only returns containsY = ['Tommy']. I tried for loop, but .find only returns the value of the first element in the provided condition, and it doesn't return all names with "y"s. Is there any better way to get all of "Y" and "y" names?

const students = ["Tommy","Noah","Xander","Adil","Bradley","Nicholas","Damien"]

let containsY = students.filter((student) => student.includes("Y"))

//Look for name with "y", and push it to containsY
containsY.push(students.find((student) => student.includes("y")))

console.log(containsY)
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Your filter approach is fine. It's giving incorrect results because includes is case sensitive.

Instead, inside the filter callback, convert student to lowercase, then check whether it includes "y":

const students = ["Tommy","Noah","Xander","Adil","Bradley","Nicholas","Damien"]

const res = students.filter((student) => student.toLowerCase().includes("y"))

console.log(res)

about 4 years ago · Juan Pablo Isaza Relatório

0

Either use a regular expression (which has the benefit of only requiring a single test)

const students = ["Tommy", "Noah", "Xander", "Adil", "Bradley", "Nicholas", "Damien"]
const result = students.filter(student => /y/i.test(student))
console.log(result)

Or test for both .includes('y') and .includes('Y') in (a single) callback.

const students = ["Tommy", "Noah", "Xander", "Adil", "Bradley", "Nicholas", "Damien"]
const result = students.filter(student => student.includes('y') || student.includes('Y'));
console.log(result)

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