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

168
Visualizações
regex - Filter out year from a date string in an array of objects

I'm trying to filter out year out of this array of objects (in React)

2021-12-20,
2021-12-21,
2021-12-22,
(...)
2022-01-28,
2022-01-31,
2022-02-01,

It has 100 entries.

Anyways, this is what I have so far:

     const stockDates = useMemo(() => chart && Object.keys(chart['Time Series (Daily)']).reverse(), [chart]);


      let filtered = stockDates.replace(/2021-/g, ''); // or  for 2022  /2022-/g

      console.log(" no year: " + filtered);

I'm getting undefined when console.logging 'filtered'

Any ideas on how to get rid of the year from date?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Assuming these are date strings, you may use match here:

stockDates = ['2021-12-20', '2021-12-21', '2021-12-22'];
years = stockDates.map(x => x.match(/\d{4}/)[0]);
console.log(years);

To remove the leading year, use:

stockDates = ['2021-12-20', '2021-12-21', '2021-12-22'];
years = stockDates.map(x => x.replace(/\d{4}-/, ""));
console.log(years);

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