我想創建名爲客戶端的對象。並將其放入數組中。在此之後,請閱讀第一個客戶的名稱。JS:對象到數組
例:
client1 {nom : "marco", prenom : "chedel", adresseMail : "[email protected]"};
,並把該客戶機到像波紋管的陣列。
listClients[client1]
我做了什麼:
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);
第一:不要使用'document.write'。其次:你有一個語法錯誤。嘗試'c.addresseMail'而不是'c [addresseMail]' –
問題在於'adresseMail'沒有字符串分隔符。你可以使用'c [「adresseMail」]'。繼承人使用jQuery爲每個郵件地址創建列表項的小提琴http://jsfiddle.net/kvPWr/。 (在這個例子中,我使用點符號)我強烈建議使用console.log來查看東西和Chrome DevTools來調試,而不是document.write,它會給你更多的信息,這將有助於解決這樣的問題: ) –
@NickHusher,我正在使用document.write,因爲我的教授以這種方式進行......我可以使用什麼呢?謝謝 ! – e1che