将JavaScript对象添加到JavaScript对象 [英] Add JavaScript object to JavaScript object
问题描述
我希望在另一个JavaScript对象中包含JavaScript对象:
I'd like to have JavaScript objects within another JavaScript object as such:
Issues:
- {"ID" : "1", "Name" : "Missing Documentation", "Notes" : "Issue1 Notes"}
- {"ID" : "2", "Name" : "Software Bug", "Notes" : "Issue2 Notes, blah, blah"}
- {"ID" : "2", "Name" : "System Not Ready", "Notes" : "Issue3 Notes, etc"}
// etc...
所以,我想要问题到持有这些JavaScript对象,这样我就可以说出问题[0] .Name或者问题[2] .ID等。
So, I'd like "Issues" to hold each of these JavaScript objects, so that I can just say Issues[0].Name, or Issues[2].ID, etc.
我创建了外部问题JavaScript对象:
I've created the outer Issues JavaScript object:
var jsonIssues = {};
我需要向其添加JavaScript对象,但不要知识。我希望能够说:
I'm to the point where I need to add a JavaScript object to it, but don't know how. I'd like to be able to say:
Issues<code here>.Name = "Missing Documentation";
Issues<code here>.ID = "1";
Issues<code here>.Notes = "Notes, notes notes";
有没有办法做到这一点?谢谢。
Is there any way to do this? Thanks.
UPDATE:根据给出的答案,声明一个数组,并根据需要推送JavaScript对象:
UPDATE: Per answers given, declared an array, and am pushing JavaScript objects on as needed:
var jsonArray_Issues = new Array();
jsonArray_Issues.push( { "ID" : id, "Name" : name, "Notes" : notes } );
感谢您的回复。
推荐答案
var jsonIssues = []; // new Array
jsonIssues.push( { ID:1, "Name":"whatever" } );
// "push" some more here
这篇关于将JavaScript对象添加到JavaScript对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!