Consider the code below:
componentDidMount(){
const id = localStorage.getItem('id');
fetch(`http://localhost:9000/api/calendar/list/${id}`,)
.then((resp)=>{
resp.json().then((res)=>{
this.setState({
data: res.data.map(item => {
return {
title: item.title,
startDate: new Date(item.startDate),
endDate: new Date(item.endDate),
eventId: item.eventType // from api it provide on string but I need to convert it in to an integer
};
})
});
})
})
}
The result from the API are here:
So on the frontend js, I need to set the eventId to a number like Conference in num 1, Launching in num 2, Trade Shows in num 3. Is it any way I can do it ?
You can create an object to map the string to a number like this, where
item1
anditem2
are examples of what your API data could be:In your code this would be