Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

147
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda