Home > OS >  How do I get data from an object that is inside an array that is inside another object in React Nati
How do I get data from an object that is inside an array that is inside another object in React Nati

Time:06-24

I have an API that contains an object. Inside that object is an array that also contains another object. I need to get the data inside the object that is inside the array and output it in a FlatList.

The data in the API:

Object {
  "groupData": Array [
    Object {
      "id": 4,
      "name": "Group A",
      "point": "3.00",
    },
    Object {
      "id": 5,
      "name": "Group B",
      "point": "1.00",
    },
  ],
}

This is my code to fetch the API

const [apiData, setApiData] = React.useState();
fetch(URLs._groupProfile, {
            method: "GET",
            headers: {
                'Content-Type': 'application/json',
                "Accept": "application/json",
                'Authorization': inputs.userToken,
            },
            }).then((response) => response.json())
            .then((result) => {
                setApiData(result.profilesData);
                console.log(apiData.id); 
                console.log(apiData.name); 
                console.log(apiData.points);
            });

CodePudding user response:

You would pass apiData.groupData in the data attribute of the FlatList.

return <FlatList data={apiData.groupData} keyExtractor={(item) => item.id.toString()} renderItem={GroupDataItem} />

CodePudding user response:

you can simply destructure result using beneath line of code

const { groupData } = result;
console.log(groupData, 'your result in simple array of objects');
  • Related