Question

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 : "ssss@ggg.com"};

and put this client into an array like bellow.

listClients[client1]

what i did :

var listClients = [];
var client1 = {nom : "chedel",prenom:"Marco",adresseMail:"marco@gmail.com"};
listClients.push(client1);
var client2 = {nom : "De Almeida",prenom:"Jorge",adresseMail:"jorge@gmail.com"};
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);
Was it helpful?

Solution

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

use

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

OTHER TIPS

There are two ways to access properties of an object:

obj.prop
obj['prop']

You are doing the following mixture which doesn't work: obj[prop].

Fix your code to c.adresseMail or c['adresseMail'] and it will work.

Either reference it using a string:

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

or using dot notation:

document.write(c.adresseMail);

And, yes - document.write should be avoided.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top