Trying to do all of this with as much vanilla Javascript as possible without needing React, Angular or anything else. I'm rendering the HTML contents through my JS file. How do I do a for loop within the template literals ( ` )? I cannot get a loop working and I need it for live data display. With the code you see on the JS file I manually put in a 0 index to get the first astronaut from the array and it works and displays fine.
This is my JS file.
const apiData ={
url: 'http://api.open-notify.org/astros.json'
}
const {url} = apiData
const apiUrl = `${url}`
fetch(apiUrl)
.then( (data) => data.json() )
.then ( (astronauts) => createHtml(astronauts) )
const createHtml = (data) => {
console.log(data)
const html = `
<div>
Total People In Space: ${data.number} <br>
Names: ${data.people[0].name} <br>
Craft: ${data.people[0].craft}
</div>
`
const astronautDiv = document.querySelector('.astronaut')
astronautDiv.innerHTML = html
}
Here is the latest contents from the API in its JSON format.
{"number": 3, "people": [{"craft": "ISS", "name": "Chris Cassidy"}, {"craft": "ISS", "name": "Anatoly Ivanishin"}, {"craft": "ISS", "name": "Ivan Vagner"}], "message": "success"}
Heres my HTML file.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Space</title>
<link rel="stylesheet" href="static/css/styles.css">
<script src="static/js/main.js"></script>
</head>
<body>
<div class="astronaut"></div>
</body>
</html>