Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

91
Visualizações
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 Respostas
Responde à pergunta

0

that...

const s = "123 hiu 234234"

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

about 4 years ago · Juan Pablo Isaza Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda