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

106
Views
filter and regex issue

I want to filter an array with a function.

if (this.pendingItems.customer?.emailAddresses) {
  this.pendingItems.customer.emailAddresses.filter(email => {
    isEmailValid(email);
  });
}

the function isEmailValid()

export function isEmailValid(s: string): boolean {
  const re =
    /^[a-z0-9+-][a-z0-9_+-]*(\.[a-z0-9_+-]+)*@([a-z0-9-]+\.)+[a-z0-9-]{2,}$/i;
  return re.test(s);
}

return the correct response (true or false) with the emails(test@email.com and test2email) I pass it, but the filter don't work.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You need to modify your expression slightly. The method you pass to the filter function should return true/false but it always returns void (which will be cast to false).

filter(email => isEmailValid(email))
or
filter(isEmailValid)
or
filter(email => { return isEmailValid(email) })

In case you missed it - you forgot to return the result of the isEmailValid ;)

about 4 years ago · Juan Pablo Isaza Report

0

So the solution was:

if (this.pendingItems.customer?.emailAddresses) {
  this.pendingItems.customer.emailAddresses = this.pendingItems.customer.emailAddresses.filter(isEmailValid);
}

I forgot to reassign the variable ;p

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!