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

223
Visualizações
How to get First element from a map that includes something?

I am trying to select the first path directory from the api that includes an Image so I can set the value to the Image source and display the first image but when i use fl[0] i get all values

here is the code :

{useLesson &&
            useLesson.files.map((fl, index) => {
               
                if (fl.mimetype.includes("image")) {
               // this always get all values not only the first value
                console.log(fl[0])
               // and this always gives me infinite loop issue
                setSelectedImage(fl[0])
                    return (
                        <div key={index}>
                                <img  src={axiosUse + fl.path} alt="" />
                        </div>

                    )
                }
            })
        }

How can I get only the first value from a map that includes something inside it ?

what I am trying to do is getting files from the server and if its an image file then display some component but how can I get only the first Image value file ?

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

0

You could create a new component to show the first file

const FilePreview = ({ mimetype, files, basepath }) => {
  const firstFileOfType = files.find((file) =>
    file.mimetype.includes(mimetype)
  );
  if (!firstFileOfType) return null;

  return (
    <div>
      <img src={basepath + firstFileOfType.path} alt="" />
    </div>
  );
};

    

and use it as

  {useLesson && (
    <FilePreview
      mimetype="image"
      files={useLesson.files}
      basepath={axiosUse}
    />
  )}
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