Hello folks!
I have the following JSON file. I must iterate over the ID to display its data on screen (as id and name_on_card).
{
"count_page": 1,
"data": [
{
"id": 104,
"card_last_numbers": "XXXX-XXXX-XXXX-4444",
"name_on_card": "sadfsad432",
},
{
"id": 103,
"card_last_numbers": "XXXX-XXXX-XXXX-4444",
"name_on_card": "asdfasdf",
},
{
"id": 102,
"card_last_numbers": "XXXX-XXXX-XXXX-4444",
"name_on_card": "asdf",
}
]
}
and I have the following function to be able to paint some data on the screen.
function loadCreditCard() {
fetch(`${ base_url }creditcard.xyz/api/card/list/?id-company={{company.id}}`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
"Authorization": `Bearer ${localStorage.getItem("jwtToken")}`,
"X-CSRFToken": "{{csrf_token}}"
},
})
.then(function (response) {
if (response.ok) {
return response.json()
} else {
return Promise.reject(response)
}
})
.then(function (data) {
document.getElementById("id_name").innerHTML = data.data[0].name_on_card;
document.getElementById("id_card_number").innerHTML = data.data[0].card_last_numbers;
document.getElementById("id_address").innerHTML = data.data[0].address;
document.getElementById("id_zip_number").innerHTML = data.data[0].zip;
document.getElementById("expire_date_years").innerHTML = data.data[0].expire_date.substring(0,4);
document.getElementById("expire_date_months").innerHTML = data.data[0].expire_date.substr(-5);
globalThis.GLOBAL_CARD_ID = data.data[0].id;
console.log(GLOBAL_CARD_ID);
})
.catch(function (err) {
console.log(err)
setTimeout(() => {
loadCreditCard()
}, 3000)
});
}
loadCreditCard()
But how can I iterate over that [0]?
Thanks in advance/