I have some code that displays an array from a json object based on the id.
I’d like to write a function that allow users to click a button see the next item (id 1), then the one after (id 2), etc. What approach should I take?
JSON:
[
{
"id": 0,
"last_name": "Dan",
"first_name": "Terry"
},
{
"id": 1,
"last_name": "Cal",
"first_name": "Adam"
},
{
"id": 2,
"year": 2015,
"last_name": "Brooke",
"first_name": "Lily"
}
]
JS:
fetch('data.json')
.then(function (response) {
return response.json();
})
.then(function (data) {
appendData(data);
})
.catch(function (err) {
console.log(err);
});
function appendData(data) {
var mainContainer = document.getElementById("myData");
var taser
for (var i = 0; i < data.length; i++) {
if (data[i].id === 0) {
var div = document.createElement("div");
div.innerHTML = data[i].first_name + data[i].last_name;
mainContainer.appendChild(div);
}
}
}
HTML:
<body>
<div id="myData"></div>
<button id="btn">next</button>
</body>
2
Answers
I think your looking for something like this.
async / await
currentUser
index to get the user data from: