When you use promises (ie.
fetch()
) you can only access the data within the
then()
function. The data produced from that API is a JSON array of objects, so you can iterate over the values as so:
fetch('https://jsonplaceholder.typicode.com/posts')
.then(response => response.json())
.then(data => {
for (const item of data) {
console.log(item.title);
}
});
Or you can use a more modern
async
/
await
approach:
const response = await fetch('https://jsonplaceholder.typicode.com/posts');
const data = response.json();
for (const item of data) {
console.log(item.title);
}