what I'm trying to do is to access a property inside a variable: for example I want to access the first and last name of the first user (index 0, nom and prenom) like this knowing that users are created dynamically:
I tried several solutions on the Internet, but none of them worked properly or did not match to my problem.
Source code :
var liste = new Array();
var count = liste.length;
class Personne{
constructor(){}
info = {
nom: faker.name.firstName(),
prenom: faker.name.lastName(),
civilite: faker.address.countryCode(),
};
}
var pers = new Personne(); //global var now
function foo() {
faker.locale= 'fr';
pers = new Personne();
document.getElementById("nom-prenom").textContent = "Nom complet : " + pers.info.nom + " " + pers.info.prenom;
document.getElementById("nationalite").textContent = "Civilité : " + pers.info.civilite;
liste.push(pers.info);
count = count + 1;
document.getElementById("count").textContent = `Liste des utilisateurs [${count}] :`;
console.log(liste)
document.getElementById("liste_util").innerHTML += `<br><button id="a${liste.length - 1}" onclick='GetPos()'>${pers.info.nom + " " + pers.info.prenom}</button>`
return {
compt :count,
personne : pers,
};
}
function GetPos()
{
alert(pers.info["nom"]); //here but for index 0
}
Thank you for the help.
`? There is a _lot_ here that you want to post to codereview about to get some help writing better (and modern) JS. – Mike 'Pomax' Kamermans Apr 15 '21 at 20:05