如果我有一個對象myObj
JS對象
,我想這個對象擁有財產性客戶將舉行clientObjects的清單,Id和Name屬性
var client= { Id: ", Name: "};
var myObj= {
clients: ????
}
我怎麼能填充客戶的屬性,並用它在myObj.clients
如果我有一個對象myObj
JS對象
,我想這個對象擁有財產性客戶將舉行clientObjects的清單,Id和Name屬性
var client= { Id: ", Name: "};
var myObj= {
clients: ????
}
我怎麼能填充客戶的屬性,並用它在myObj.clients
可能這是你想要的
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);
請仔細閱讀這一個:
//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}`);
})
可能,這將有助於你.....
var myObject = {clients:[]};
myObject.clients.push({id:"1",name:"name"});
myObject.clients.push({id:"2",name:"name"});
console.log(myObject);
看跌的客戶名單到數組 – Mahi
'客戶端: []'然後使用'myObj.clients.push'來添加 – Rajesh
爲什麼downvoting的人。他是js中的新人。 – Mahi