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

103
Visualizações
Find first element of array and show it in map function but have warning - React

I have data like so:

    {
      "title": 0,
      "name": 0,
      "score": 0,
      "summary": [
        {
          "id": 1,
          "start": "2019-11-01",
          "end": "2019-11-04"
        },
        {
          "id": 2,
          "start": "2019-11-01",
          "end": "2019-11-04"
        }
      ]
    }

I am trying to access the first element of summary, so I have tried this:

    const newDate = new Date(data?.summary.map((d: any, index: number) => {
               if (index === 0) {
                 return (
                 new Date(d.start)
               )
          }
     })),

although it's working i get the warning:

Array.prototype.map() expects a value to be returned at the end of arrow function array-callback-return

I know it must be because I am not returning anything in the callback and the return is in the if statement?

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

0

if (index === 0) {
                 return (
                 new Date(d.start)
               )

You are only returning a value if index === 0.

Im not sure what are you trying to achieve:

const newDate = new Date(data?.summary.map((d: any, index: number) => {
           if (index === 0) {
             return (
             new Date(d.start)
           )
      }
 })),

I suppose:

const newDate = newDate(data?.summary[0]?.start) 

Will do the thing.

about 4 years ago · Juan Pablo Isaza Relatório

0

You can access to the first element without using map function:

const newDate = new Date(data?.summary[0].start)
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