2016-11-22 50 views
-3

如果我有一個對象myObjJS對象

,我想這個對象擁有財產性客戶將舉行clientObjects的清單,Id和Name屬性

var client= { Id: ", Name: "}; 
var myObj= { 
     clients: ????  
} 

我怎麼能填充客戶的屬性,並用它在myObj.clients

+1

看跌的客戶名單到數組 – Mahi

+0

'客戶端: []'然後使用'myObj.clients.push'來添加 – Rajesh

+0

爲什麼downvoting的人。他是js中的新人。 – Mahi

回答

0

可能這是你想要的

var myObj = {}; // Your 'myObj' object 
 

 
// 'clients' property which will hold list of clientObjects 
 
myObj.clients = [{ 
 
    id: "1", 
 
    Name: "name1", 
 
}, { 
 
    id: "2", 
 
    Name: "name2" 
 
}]; 
 

 
console.log(myObj);

+0

我們需要這個:'myObj.clients = [];'? – Rajesh

+0

我剛分解成簡單的步驟,只是爲了說明OP –

+0

這會混淆初學者,因爲該行不是必需的。 – Rajesh

0

請仔細閱讀這一個:

//Following is Array of Client (aka object). Thats all you need 
 
var clientList = [ 
 
    { Id: "id1", Name: "Name1"}, 
 
    { Id: "id2", Name: "Name2"}, 
 
    { Id: "id3", Name: "Name3"}, 
 
    { Id: "id4", Name: "Name4"}, 
 
    { Id: "id5", Name: "Name5"} 
 
]; 
 

 
//and you can iterate through it in many ways, like this: 
 
clientList.forEach(function(client) { 
 
    console.log(`Client Id:${client.Id} Name:${client.Name}`); 
 
})

0

可能,這將有助於你.....

var myObject = {clients:[]}; 
myObject.clients.push({id:"1",name:"name"}); 
myObject.clients.push({id:"2",name:"name"}); 
console.log(myObject);