I'm very new to React. I've set up a Nodejs back end which reads a JSON file in the following format:
{
"cert1" : {
"name" : "www.google.com",
"state" : "valid",
"days" : "482"
},
"cert2" : {
"name" : "www.facebook.com",
"state" : "valid",
"days" : "182"
},
.
.
.
}
I want to display this data in a table and first need to put it into an array. I've managed to display www.google.com
using the following code.
class App extends Component {
state = {
name : '',
state : '',
days : '',
response : ''
};
componentDidMount() {
this.callApi()
.then(res => {
this.setState({
response: res.cert1.name
})
})
.catch(err => console.log(err));
}
callApi = async () => {
const response = await fetch('/list-certs');
const body = await response.json();
if (response.status !== 200) throw Error(body.message);
return body;
};
render() {
return (
<div className="App">
<div className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h2>Welcome to React</h2>
</div>
<p className="App-intro">
{this.state.response}
</p>
</div>
);
}
}
How do I map over the whole JSON file and populate some arrays with all the entries? Right now I'm calling res.cert1.name
but each cert entry in the JSON file has a different name (cert1, cert2, cert3 etc.) so how do I convert res.cert1.name
into a generic call for any cert entry in the JSON file?