2
I have a problem in the code section below.
<!DOCTYPE html>
<html>
<head>
<title>02 - Promises Example</title>
<style type="text/css">
.promises {
width: 100%;
font-size: 16px;
font-family: cursive;
}
</style>
</head>
<body>
</body>
<script>
var posts = fetch('https://willianjusten.com.br/search.json');
// pending
// resolved
// rejected
posts
.then(data => data.json())
.then(data => data.reduce(post => {
var teste = post.title;
console.log(teste);
const markup = `<div class="promises">${teste}</div>`;
document.body.innerHTML = markup;
}));
</script>
</html>
I need to display all the elements on the screen, but it only displays the last element of the array.
I want to have flexibility to display all the content of json, or all categories, for example: all fields "date" or all fields "title" and so on, can give me a help?
reduce
not server to iterate arrays the way you are using, try to usedata.forEach(post => {
, and then concatenate the content withdocument.body.innerHTML += markup
– Andre