53

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.

Crescent Fresh
  • 115,249
  • 25
  • 154
  • 140
Matt
  • 23,363
  • 39
  • 111
  • 152
  • 10
    There are a lot of answers instructing you to convert to an array (and I see you did in the end). I know this is an old question, but there is a new solution. For anybody coming to this looking for an answer to the question of how to add an object to an object, please look at Object.assign. (it's new but this question comes up high on searches) I spent a lot of time looking for a solution to this that didn't involve creating an array and then converting it back. Here is the spec: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign – O Genthe Nov 28 '20 at 18:18

7 Answers7

62
var jsonIssues = []; // new Array
jsonIssues.push( { ID:1, "Name":"whatever" } );
// "push" some more here
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
jldupont
  • 93,734
  • 56
  • 203
  • 318
  • 1
    @jldupont, Thanks for the answer! I wasn't thinking in terms of arrays when I asked the question. But this makes sense. – Matt Nov 17 '09 at 17:12
  • would this work for unshift also because i wanted to add the object on the first position. – Sagar Devanga Sep 23 '14 at 07:08
41

As my first object is a native JavaScript object (used like a list of objects), push didn't work in my scenario, but I resolved it by adding new key as follows:

MyObjList['newKey'] = obj;

In addition to this, may be useful to know how to delete the same object as inserted before:

delete MyObjList['newKey'][id];

Hope it helps someone as it helped me.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
Alex
  • 643
  • 7
  • 17
9
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.

Zoidberg
  • 10,137
  • 2
  • 31
  • 53
6

// Merge object2 into object1, recursively

$.extend( true, object1, object2 );

// Merge object2 into object1

$.extend( object1, object2 );

https://api.jquery.com/jquery.extend/

Eden
  • 107
  • 1
  • 4
3

If it's not an array of object you can do this:

let student= {
  name : 'Mr. Anderson',
  id: 35
}

student['grade'] = 10; //for a property. 

Result:

student= {
  name : 'Mr. Anderson',
  id: 35,
  grade:10
}

You also can add an object:

let student= {
 personalData:{
  //personal data key-value
 }
}

let academicData = {
 //academic data key-value
}

student['academicData'] = academicData;

Result:

 student{
        personalData{},
        academicData{}
    }
Scoreby
  • 81
  • 4
1
jsonIssues = [...jsonIssues,{ID:'3',Name:'name 3',Notes:'NOTES 3'}]
Ooker
  • 1,969
  • 4
  • 28
  • 58
Striker
  • 331
  • 3
  • 6
0

If you have properties in first obj and you have to add your objs to it, then Object.assign() will erase it.

To avoid this loss I've written a function below. Be aware, it copies nested objs by refference.

First you should add all objs you want to add to your's obj to an arr. You can do it easily by arr.push(). Then just use the Fn.

function addMyObjs (objInit, arrWithObjs) {
    let map = new Map();
    for (let [key, value] of Object.entries(objInit)) {
        map.set(key, value);
    }
    arrWithObjs.forEach((item) => {
        for (let [key, value] of Object.entries(item)) {
            map.set(key, value);
        }
    });
    return Object.fromEntries(map);
}

let objSecond = {id: 2,};

let obj = {
    name: "Tolya",
    age: 33,
    sex: "man",
};

let obj3 = {"fruits": {"apples": true, "plums": false,}};

let arr = [obj, obj3];

objSecond = addMyObjs(objSecond, arr);