I want to work with an JSON File in Javascript I got from my Flask server via a Axios Get-Request.
Its build like this:
{
"data": [
{
"item": "[224,4,1,80,207,137,153,132]",
"name": "A1"
},
{
"item": "[224,4,1,80,207,137,153,136]",
"name": "A2"
},
{
"item": "[224,4,1,80,207,137,157,190]",
"name": "A3"
}
]
}
I make my request with this and I can see the output in the terminal:
getInitialBoxesA() {
const path = 'http://localhost:5000/getAllBoxesA';
axios.get(path)
.then((res) => {
console.log(res.data);
})
.catch((error) => {
// eslint-disable-next-line
console.error(error);
});
},
How can i iterate through this Array in Javascript?
I tried res.data[0] or res.data[0].item
Axios returns the actual response body in res.data. Given your JSON, you would need to iterate on res.data.data.
axios.get(path)
.then((res) => {
if (res.status === 200 && res.data?.data.length) {
for (obj of res.data.data) {
console.log(obj)
}
}
})
.catch((error) => {
// eslint-disable-next-line
console.error(error);
});