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

210
Visualizações
How to map the first object in an array in react?

I am mapping an array of object in react. The array is as follows

const tabs = [
    {
        index: 0,
        title: 'v1',
        path: '/v1',
        component: versionManager,
    },
    {
        index: 1,
        title: 'v2',
        path: '/v2',
        component: version2Manager,
    },
    {
        index: 0,
        title: 'v3',
        path: '/v3',
        component: version3Manager,
    },
];

I have successfully mapped the entire array with this

{tabs.map((item) => {
           if (auth.verify(Roles.role1)) {
                   return (
                        <Tab
                             label={item.title}
                             key={item.index}
                             component={Link}
                             to={item.path}
                         />
                    );

but I would like to add an else that only maps the first object (v1) and all of its elements, something similar to this.

} else {
       return (
             <Tab
                 label={item.title}
                 key={item.index}
                 component={Link}
                 to={item.path}
              />
                )
 }

I have tried thing such as item.title[0], item.index[0] ,etc.... but it gives an undefined error every time. Does anyone know the best way to only map the first object in the else statement? Thanks in advance.

I have seen Get first object from array of objects in react but this didn't seem to be helpful in my case.

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

0

item.title[0], item.index[0] won't work because item is only an object from the tabs array. What you want is:

} else {
       return (
             <Tab
                 label={tabs[0].title}
                 key={tabs[0].index}
                 component={Link}
                 to={tabs[0].path}
              />
                )
 }
about 4 years ago · Juan Pablo Isaza Relatório

0

You want to get the first element of the tabs array, but you've been trying to access the first element of the properties of the array as if the properties are arrays themselves, and tabs is just a regular object.

So you'd want to use tabs[0].title, tabs[0].index, etc.

about 4 years ago · Juan Pablo Isaza Relatório

0

Map always iterates over all of the items of the array. Another way you can try is to first check if the user has the role you need. If not, just return the first tab.

  if(auth.verify(Roles.role1)){
    return tabs.map((item) => {
      return (<Tab
              label={item.title}
              key={item.index}
              component={Link}
              to={item.path}
              ​/>);
     }
      } else {
         return (
         <Tab
          label={tabs[0].title}
          key={tabs[0].index}
          component={Link}
          to={tabs[0].path}
         />)
}
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