there is Api - an array:
[
{"id":1,"firstName":"Alex","lastName":"Creel","email":"[email protected]","phone":"(898)979-8452"},
{"id":2,"firstName":"Todd","lastName":"Drek","email":"[email protected]","phone":"(898)979-8452"},
{"id":3,"firstName":"Jim","lastName":"Sparou","email":"[email protected]","phone":"(898)979-8452"},
{"id":4,"firstName":"Tom","lastName":"Limls","email":"[email protected]","phone":"(898)979-8452"},
{"id":5,"firstName":"Jack","lastName":"Retnd","email":"[email protected]","phone":"(898)979-8452"}
]
using fetch, I get Api - an array that becomes a data object (I don’t write App.js code so that the question is not too long) and using the map method, in the form of a table, I display the data I need on the page:
export default props => (
<table className="table">
<thead>
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
</tr>
</thead>
<tbody>
{props.data.map(item => (
<tr key={item.id}>
<td>{item.id}</td>
<td>{item.firstName}</td>
<td>{item.lastName}</td>
</tr>
))}
</tbody>
</table>
)
there will be such a table on the screen:
But I have this second strange Api:
{"group":{"first":[
{"id":1,"firstName":"Alex","lastName":"Creel","email":"[email protected]","phone":"(898)979-8452"},
{"id":2,"firstName":"Todd","lastName":"Drek","email":"[email protected]","phone":"(898)979-8452"},
{"id":3,"firstName":"Jim","lastName":"Sparou","email":"[email protected]","phone":"(898)979-8452"},
{"id":4,"firstName":"Tom","lastName":"Limls","email":"[email protected]","phone":"(898)979-8452"},
{"id":5,"firstName":"Jack","lastName":"Retnd","email":"[email protected]","phone":"(898)979-8452"}
]}}
and I have a question how can I change this part of the code:
{props.data.map(item => (
<tr key={item.id}>
<td>{item.id}</td>
<td>{item.firstName}</td>
<td>{item.lastName}</td>
</tr>
how to make the map method work with my second api on the screen and get exactly the same list of names as with the first api?
I tried like this:
{props.data.group.first.map(item =>
But then the error: TypeError: Cannot read property 'first' of undefined