this is my first post and was extremely confused on how to map over the json data correctly.
The problem comes from when you map the user address property with Object.keys, everything is mapping over fine until it gets to the "geo" property values. What is the solution to map and render over every property easier?
const style = {
list: {
listStyle: "none"
}
};
const data = [
{
id: 1,
name: "Leanne Graham",
username: "Bret",
email: "[email protected]",
address: {
street: "Kulas Light",
suite: "Apt. 556",
city: "Gwenborough",
zipcode: "92998-3874",
geo: {
lat: "-37.3159",
lng: "81.1496"
}
},
phone: "1-770-736-8031 x56442",
website: "hildegard.org",
company: {
name: "Romaguera-Crona",
catchPhrase: "Multi-layered client-server neural-net",
bs: "harness real-time e-markets"
}
}
];
function App() {
return (
<div className="App">
<ul style={style.list}>
{data.map(user => {
return (
<Fragment key={user.id}>
<li>{user.username}</li>
<ul style={style.list}>
{Object.keys(user.address).map(key => {
return (
<li>
{key} {user.address[key]}
</li>
);
})}
</ul>
</Fragment>
);
})}
</ul>
</div>
);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
[<>]toolbar button). Stack Snippets support React, including JSX; here's how to do one.user.address[key]will eventually return thegeoobject, and React doesn't render objects by itself.