vote up 0 vote down star

Hi all,

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...

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.

I've created the outer Issues JavaScript object:

var jsonIssues = {};

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: 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 } );

Thanks for the responses.

flag

2 Answers

vote up 2 vote down check
var jsonIssues=new Array();
jsonIssues.push( { ID:1, "Name":"whatever" } );
// "push" some more here
link|flag
@jldupont, Thanks for the answer! I wasn't thinking in terms of arrays when I asked the question. But this makes sense. – Mega Matt Nov 17 at 17:12
@Mega Matt: my pleasure. – jldupont Nov 17 at 17:13
vote up 2 vote down
var jsonIssues = [
 {ID:'1',Name:'Some name',Notes:'NOTES'},
 {ID:'2',Name:'Some name 2',Notes:'NOTES 2'}
];

If you want to add to the array then you can do this

jsonIssues[jsonIssues.length] = {ID:'3',Name:'Some name 3',Notes:'NOTES 3'};

Or you can use the push technique that the other guy posted, which is also good.

link|flag
So essentially, I'll have an array of JSON objects? – Mega Matt Nov 17 at 17:07
I believe the OP would also like to know how to add objects to what you have. – Crescent Fresh Nov 17 at 17:07
You'll have an array of JavaScript objects. JSON is just a serialization that happens to conform to a subset of JavaScript. – David Dorward Nov 17 at 17:11
Yes, sorry, you can add to it as if it was an array built manually – Zoidberg Nov 17 at 17:19

Your Answer

Get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.