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

139
Visualizações
Should I map an array if there is only one object in it?

I am trying to use data I fetch from an Api in a react project. The api returns an array with only one object, but that object has many properties I need to use in the jsx.

My question is more about efficiency/best practice. Should I map the array or just call index 0.

This is within the return of the component. Just to reiterate the array only has the ONE object which has a bunch of properties.

array.map((obj) => (
   <p>{obj.id}</p>
   <p>{obj.name}</p>
   <p>{obj.other}</p>
   <p>{obj.title}</p>
)) 

or

<p>{array[0].id}</p>
<p>{array[0].name}</p>
<p>{array[0].other}</p>
<p>{array[0].title}</p>
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

If the API is not going to change, and always return an array with an entry, just set your state accordingly:

fetch('/api')
  .then(response => response.json())
  .then(data => setSomething(data[0]))

If the API can return multiple entries, you will need .map() for semantics.

about 4 years ago · Juan Pablo Isaza Relatório

0

Calling index 0 would be slightly more performant than starting an iteration, getting the one object, then stopping the iteration. However if you will ever expect there to be more than one object then you will need map.

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