Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

152
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!