-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
46 lines (41 loc) · 1.2 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
const container = document.getElementById("container");
async function getPost() {
const responsePosts = await fetch(
"https://jsonplaceholder.typicode.com/posts"
);
const posts = await responsePosts.json();
return posts;
}
async function getComments() {
const responseComments = await fetch(
"https://jsonplaceholder.typicode.com/comments"
);
const comments = await responseComments.json();
console.log(comments);
return comments;
}
function writePostAndComments() {
Promise.all([getPost(), getComments()]).then(([posts, comments]) => {
posts.forEach((post) => {
let filteredComments = comments.filter((comment) => {
return comment.postId == post.id;
});
let mappedComments = filteredComments
.map((comment) => {
return `<div style="background-color: red">
<span>Nome:${comment.name} </span <br>
<span>Email:${comment.email} </span> <br>
<span>Commento:${comment.body} </span>
</div>`;
})
.join("");
container.innerHTML += `<div class="post">
<h1>${post.title}</h1>
<br>
<span>${post.body}</span> <br>
${mappedComments}
</div>`;
});
});
}
writePostAndComments();