I am having difficulty with the syntax and the structure of JSON objects/arrays and map method. I am new to React and on an initial stage of learning.
This is the JSON file code I pasted below:
[
{
"cloud":"Asia",
"availability":{
"last15Min":"100%",
"last24Hour":"100%"
},
"data_centers":[
{
"title":"Bombay",
"availability":{
"last15Min":"100%",
"last24Hour":"100%"
}
},
{
"title":"Bombay1",
"availability":{
"last15Min":"100%",
"last24Hour":"100%"
}
}
]
},
{
"cloud":"Europe",
"availability":{
"last15Min":"100%",
"last24Hour":"100%"
},
"data_centers":[
{
"title": "Bombay",
"availability": {
"last15Min": "100%",
"last24Hour": "100%"
}
},
{
"title":"Bombay1",
"availability":{
"last15Min":"100%",
"last24Hour":"100%"
}
}
]
}
]
Here is my code so far: I want to render each field using map method. What is the correct method to do that?
In componentDidMount I am getting the response in console.log http://prntscr.com/i09rqt
constructor(props) {
super(props)
this.state = {
clouds:[]
}
}
componentDidMount() {
var url="<api url>";
fetch(url)
.then(response => {
return response.json();
}).then(d => {
let clouds = d.map((cloudData)=>{
return(
<div>{cloudData}</div>
)
})
this.setState({clouds: clouds});
console.log("state", this.state.clouds)
})
}
render() {
return (
<div>
{
this.state.clouds.map((cloud =>
<th key="">
{cloud}
</th>
))
}
</div>
);
}