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

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

0

that...

const s = "123 hiu 234234"

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

about 4 years ago · Juan Pablo Isaza Denunciar

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 Denunciar

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 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