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

90
Views
Combining a filter and a map

Is it possible to combine a map and a filter in a single javascript expression? For example, I am currently doing the following to trim whitespace and remove empty results:

const s = "123 hiu 234234"
console.log(s
            .split(/\d+/g)
            .filter((item, i) => item.trim())
            .map((item, i) => item.trim())
            );

Is there a more compact way to do that? And, as a follow up question, is the /g necessary when doing split or does that automatically split every occurrence?

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

0

that...

const s = "123 hiu 234234"

console.log( s.match(/[a-z]+/ig) )

about 4 years ago · Juan Pablo Isaza Report

0

If a single word comes between the numbers, I think a single regular expression would be enough here - just match non-whitespace, non-digits:

const s = "123 hiu 234234"
console.log(s
            .match(/[^\d ]+/g)
            );

about 4 years ago · Juan Pablo Isaza Report

0

One way to do it would also be to define a function that takes two arguments that you can call which does the task. For example:

const s = "123 hiu 234234"
const Trim = (item, i) => item.trim();
console.log(s.split(/\d+/).filter(Trim).map(Trim));

Or you could put the burden on the regex itself, for example only matching letters:

s="123 hiu 234234"
console.log(s.match(/[a-zA-Z]+/g));
                //  /[a-z]+/gi alternately

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!