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

159
Visualizações
react js :fetch data from inner array and load it to state

i have a api with peoples i am tring to dispaly names of people

"peoples": [
    {
        "id": 2,
        "mainModule": "bonding",
        "description": "some random description 2",

        "persons": [
            {
                "id": 3,
                "name": "hari",
                "completed": false
            },
            {
                "id": 4,
                "name": "meenu",
                "completed": false
            }
        ]


 "id": 1,
        "mainModule": "bonding2",
        "description": "some random description 2",


   "persons": [
            {
                "id": 3,
                "name": "jisa",
                "completed": false
            },
            {
                "id": 4,
                "name": "stepy",
                "completed": false
            }
        ]
    },
]

This is my data in api

I am using axios to set a state called items

 axios
      .get(
        "url",
        config
      )
      .then((res) => {

        console.log(res.data.peoples)//this works
        console.log(res.data.peoples.persons);//here i am getting error
      

        this.setState({ items: res.data.peoples.persons}); error
}

I am trying to pass persons so I can map person names in JSX

i have changed the code pls help

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

0

Welcome to StackOverflow! As you can see in your first snippet you have an array inside peoples so the correct way to access it would be:

res.data.peoples[0].persons

Hope this solves your problem. You can read more about it in MDN docs

edit:

If you would like to iterate over persons you can do this:

res.data.peoples.map(person => (
  <div key={person.id}>{person.name}</div>
));

Remember you must pass a unique key prop to the component so React can create and mantain the list correctly. More info about it in the Docs.

about 4 years ago · Juan Pablo Isaza Relatório

0

res.data.peoples is an array so you should iterate over it. It doesn't have persons property, it has [0], [1] etc. Try to log res.data.peoples[0].persons

about 4 years ago · Juan Pablo Isaza Relatório

0

From the sample data you showed, peoples is an array. So to access persons, you should be able to access it using the following, for example if you want to access the first element of the array:

res.data.peoples[0].persons

Otherwise, if you want to access it as res.data.peoples.persons then you have to change your API output such that it is like so :

"peoples": {
        "id": 2,
        "mainModule": "bonding",
        "description": "some random description 2",

        "persons": [
            {
                "id": 3,
                "name": "hari",
                "completed": false
            },
            {
                "id": 4,
                "name": "meenu",
                "completed": false
            }
        ]
    }
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