first of all, i have to say that i´m quiet new to javascript and Frontend, especially with React.
I try to do an API-Call to get the JSON and to display it via React. I´m using a material-ui template which just needs the informations for the display in form of two arrays, one for the data and one for the headers.
I would like to get the "name" Properties of the JSON into one of this arrays.
I wrote a state with the useState() function. Inside the (fetch.then(data) =>) i would like to assign the json from the api to the state value. But as i just want to print a part of the Json i tried to data.value[0].value, but it won´t work.
it says: "TypeError: data.holidays[i] is undefined" as i want to acess via data.holidays[i].name
//Create State
const [data, setData] = useState({
holidays: [holiday],
});
const [holiday, setHoliday] = useState({
id: "",
logo: "",
name: "",
});
fetch("http://localhost:8080/da-calendarific/api/regionGroup/all")
.then((response) => response.json())
.then((data) => {
setData({ holidays: data });
});
//This should be the Output Array
let returnvalue = new Array();
var i;
for(i = 0; i < data.holidays.length; i++) {
returnvalue.push(data.holidays[i].name);
}
JSON from API:
[
{
"id": 1,
"name": "Europe\n"
},
{
"id": 3,
"logo": ".png\n",
"name": "Asia\n"
},
{
"id": 4,
"logo": ".png\n",
"name": "Australia\n"
},
{
"id": 5,
"logo": ".png\n",
"name": "America\n"
}
]