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

183
Views
How to remove elements from array if not matching?

I am trying to delete all elements that matches @b.com, but it returns an empty array even if I remove the !, which leads me to think, I am doing something competently wrong.

Can someone explain what I am doing wrong?

    const arr = ['a@a.com', 'b@b.com', 'c@c.com'];
    
    if (arr !== null && arr.length > 0) {
       const arr2 = arr.filter(e => {
          !e.match(/@b.com/);
       });
       console.log(arr2);
    }

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

0

You forgot return statement:

const arr = ['a@a.com', 'b@b.com', 'c@c.com'];

if (arr !== null && arr.length > 0) {
   const arr2 = arr.filter(e => {
      return !e.match(/@b.com/);
   });
   console.log(arr2);
}

One more thing, if you don't match by regex, you can just use string.includes:

const arr = ['a@a.com', 'b@b.com', 'c@c.com'];

if (arr !== null && arr.length > 0) {
   const arr2 = arr.filter(e => {
      return !e.includes('@b.com');
   });
   console.log(arr2);
}

about 4 years ago · Juan Pablo Isaza Report

0

You forget the return statement.

const arr = ['a@a.com', 'b@b.com', 'c@c.com'];
const arr2 = arr?.length ? arr.filter((e) => !e.match(/@b.com/)) : [];
console.log(arr2);

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!