Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add JavaScript object to JavaScript object

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.

like image 640
MegaMatt Avatar asked Nov 17 '09 17:11

MegaMatt


People also ask

Can you put an object in an object in JavaScript?

These properties must be unique and the differentiating factors that distinguish an object from another. Now, if you want to create an object within another object, the inner object is created as a property of the outer one, and this inner object can only be accessed using the outer object.

How do you combine objects in JavaScript?

The easiest way to merge two objects in JavaScript is with the ES6 spread syntax / operator ( ... ). All you have to do is insert one object into another object along with the spread syntax and any object you use the spread syntax on will be merged into the parent object.


1 Answers

var jsonIssues = []; // new Array jsonIssues.push( { ID:1, "Name":"whatever" } ); // "push" some more here 
like image 66
jldupont Avatar answered Sep 21 '22 03:09

jldupont