I’m using Fetch to get data from a json server and save it in the connection
constant. After getting the data, I use connection.json()
to transform the json into a javascript object. But I can’t map this object, because it is an Object with an array inside. I would like to turn it into an array of objects. How should I proceed?
useEffect(() => {
list();
}, [ ]);
async function list(){
try{
const connection = await fetch("https://my-json-server.typicode.com/priscilasilvaalves/priscilasilvaalves.github.io/db");
const list = await connection.json();
setItems(list);
console.log(items); // Returns an Object with an array inside
}catch(error){
console.log(error);
}
}
2
Answers
the response contains the node lista. If you access it directly, you will get the desired result.
if you want to render the array of objects,
you can use something like this,