The fetch works fine, but the "title" array has no values in it after pushing the values from the fetch data.
function getdata(){
const title = [];
const body = [];
const url = 'https://jsonplaceholder.typicode.com/posts';
fetch(url)
.then(response => response.json())
.then(data => {
// console.log(data) /*This works just fine*/
data.forEach(posts => {
title.push(posts.title)
body.push(posts.body)
})
})
// const onlytentitle = title.slice(0,9);
// return onlytentitle;
return title;
}
const titled = getdata();
console.log(titled);