Search code examples
javascriptarraysobjectjsobject

JS : Objects into array


i'm trying to create object named client. and put it into a array. And after this, read the name of the 1st client.

Exemple :

client1 {nom : "marco", prenom : "chedel", adresseMail : "[email protected]"};

and put this client into an array like bellow.

listClients[client1]

what i did :

var listClients = [];
var client1 = {nom : "chedel",prenom:"Marco",adresseMail:"[email protected]"};
listClients.push(client1);
var client2 = {nom : "De Almeida",prenom:"Jorge",adresseMail:"[email protected]"};
listClients.push(client2);


function afficheClients(tableau){
  for (i=0;i<tableau.length;i++)
    {
      var c = tableau[i];
      document.write(c[adresseMail]); 
     // This ^^ doesn't work, it says :adresseMail isn't definied in c
    }
}

afficheClients(listClients);

Solution

  • You're treating adressMail as a variable and not as a string:

    use

    document.write(c["adresseMail"]);